How to run a script without closing the terminal?

  • I have a script in file bla.sh and it is executable. When I click on it, the script is executed and the window is closed. I'd like the window to stay open.

    Something like command cmd /k** command in Windows.

    P.S. I don't want to use pause, but I want to able to write more commands after the script was executed.

  • Put $SHELL at the end of your script:

    alt text

    A small flaw: since gnome-terminal isn't running the bash as it's shell, it will regard it as an application and display a warning about it when you try to close the terminal:

    There is still a process running in this terminal
    Closing the terminal will kill it.
    

    I've found no nice way to hide this warning. If you want, you can disable it entirely by running:

    gconftool --set /apps/gnome-terminal/global/confirm_window_close --type boolean false
    

    This doesn't happen if you're using xterm instead of gnome-terminal; should it bother you.

    You can use `exec $SHELL` instead of just `$SHELL` to make the warning go away without changing settings.

    But this solution creates an extra shell / process. Can this be avoided?

    I don't know of a way to do this, no. The problem is that you lose your handle on standard IO once the first bash process is finished, so you would have to do some trickery with a daemon that passes on these handles, which would then be more error-prone and annoying.

    On my system, `echo $SHELL` shows that the SHELL variable contains `/bin/bash`. Therefore, using `bash` in place of `$SHELL` is identical. Also, I've determined that all 3 of these at the end will keep the terminal open: `bash`, `eval bash`, `exec bash`, but only the latter, `exec bash`, will then allow you to close the terminal without it warning you a process is still running. Therefore, the best to use is `exec bash` or `exec $SHELL`, as @AndreaCorbellini says in his comment above.

  • Using Gnome Terminal

    Using gnome-terminal appending ;bash at the end of the command string and calling the script with -c option works. For example:

    gnome-terminal -e "bash -c ~/script.sh;bash"
    

    This does the following:

    1. opens gnome-terminal
    2. executes the script script.sh
    3. shows the bash prompt after the script has finished.

    You can exit the gnome-terminal window by closing the window or type exit at the bash prompt. Or you can type more commands as requested.

    `-e` is deprecated, so what do we do now? `# Option “-e” is deprecated and might be removed in a later version of gnome-terminal. # Use “-- ” to terminate the options and put the command line to execute after it. `

  • If you have access to the script, you may also add the following code at the end:

    read
    

    That code will wait for an input before closing, so the terminal will stay open until you press Enter.

    I often use `bash` at the end, but `read` allows me to close the window just with `ctrl+c` or `enter`, instead of `ctrl+shift+w` or `alt+f4`. This is convenient when you iterate rapidly on your script.

  • Use bash's --init-file option with a temporary pipe:

    bash --init-file <(echo './<script_name>')
    

    Ex:

    bash --init-file <(echo './bla.sh')
    

    What is the meaning of `<()` and how do I google this?

    `()` is `Process substitution`. `<(cmd)` creates a temp file (actually a FIFO) with the stdout of the `cmd` as its contents. For example, `cat <(echo Hi)` would just dump the o/p of `echo Hi` in a FIFO and feed the FIFO's file descriptor as input to `cat`

  • Using xterm and appending ;bash at the end of the command string works. For example:

    xterm -e "bash ~/script.sh;bash"
    

    This does the following:

    1. opens xterm
    2. executes the script script.sh
    3. shows the bash prompt after the script has finished.

    You can exit the xterm window by closing the window or type exit at the bash prompt. Or you can type more commands as requested.

  • xterm -e bash --rcfile bla.sh
    

    This will run the script in a new window, and even give you control of the window after it is finished.

    However the new window will not load ~/.bashrc as normal, since we ran bla.sh instead. This can be remedied by putting

    . ~/.bashrc
    

    at the top of bla.sh

  • There isn't an answer showing exec bash as the right answer, but there should be, so here it is. This answer is borrowed in part from @Karsvo's answer, @Stefano Palazo's answer, and @Andrea Corbellini's comment under Stefano's answer.

    On my system, echo $SHELL shows that the SHELL variable contains /bin/bash. Therefore, using bash in place of $SHELL is identical. Also, I've determined that all 3 of these at the end will keep the terminal open: bash, eval bash, exec bash, but only the latter, exec bash, will then allow you to close the terminal without it warning you a process is still running. Therefore, the best to use is exec bash or exec $SHELL, as @AndreaCorbellini says in his comment.

    Here's what this might look like in a use-case of having a script to open up two new gnome-terminal tabs already cded into your $HOME (~) directory:

    gnome-terminal --tab -- bash -ic "cd ~; exec bash;"
    gnome-terminal --tab -- bash -ic "cd ~; exec bash;"
    

    Again, you can use exec $SHELL here instead as well:

    gnome-terminal --tab -- bash -ic "cd ~; exec $SHELL;"
    gnome-terminal --tab -- bash -ic "cd ~; exec $SHELL;"
    

    Related:

    1. Here's why I REALLY needed this information! - Open Terminal with multiple tabs and execute application

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM