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

前端 未结 2 1542
轮回少年
轮回少年 2020-12-19 15:05

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:<

2条回答
  •  被撕碎了的回忆
    2020-12-19 15:36

    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
    

提交回复
热议问题