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

2019-01-26 21:36发布

问题:

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.

回答1:

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


回答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



标签: startup xterm