How to run a command on the startup of an xterm?
Asked Answered
T

2

8

How can I run a command on xterm startup i.e. when an xterm terminal is launched a the command is already executed?

I have edited the .bashrc file to add this line:

xterm "ls"

But this does not work.

Please suggest what should I do to acheive this.

Thanks.

Tensible answered 27/6, 2015 at 12:52 Comment(0)
S
6

According to the bash manual, ~/.bashrc is used for interactive shells. xterm runs a shell, so perhaps your "does not work" causes a chain of xterm's.

The xterm program sets these environment variables which are useful for scripting: XTERM_VERSION and XTERM_SHELL. In your ~/.bashrc file, you could use the former to run the xterm -ls once only:

if [[ -z "$XTERM_VERSION" ]]
then
    xterm -hold -e ls &
fi

which seems to be what you are asking for:

  • it would run an xterm if not run from an existing xterm
  • it prevents the xterm from closing when the ls is done.

A more useful-seeming way of showing an ls on shell startup would be to run ls in each shell as it is started (for that case, you do not need run a separate xterm). Again, you can use environment variables to do this once (in case you run bash to make a subshell):

if [[ -z "$XTERM_ONCE" ]]
then
    export XTERM_ONCE=$(date)
    ls
fi
Sapper answered 27/6, 2015 at 17:18 Comment(1)
xterm has set this since 2005 - see (changelog)[invisible-island.net/xterm/xterm.log.html#xterm_202]Sapper
F
2

I use this:

-e /bin/bash -login

-e command [arguments]

Run the command with its command-line arguments in the rxvt window; also sets the window title and icon name to be the basename of the program being executed if neither -title (-T) nor -n are given on the command line. If this option is used, it must be the last on the command-line. If there is no -e option then the default is to run the program specified by the SHELL environment variable or, failing that, sh(1).

http://linux.die.net/man/1/rxvt

Fefeal answered 27/6, 2015 at 12:53 Comment(3)
where do you type this -e /bin/bash -login ? I mean in which file ?Tensible
These are apparently command-line arguments for the xterm or rxvt invocation. If you don't start xterm from a command-line prompt (in which case you are already running an xterm instance in many environments!) this seems less useful.Megass
xterm -e /bin/sh -c "hello -g Example" worked for me :)Dennis

© 2022 - 2024 — McMap. All rights reserved.