74

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.

93

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.

| improve this answer | |
  • 23
    You can use exec $SHELL instead of just $SHELL to make the warning go away without changing settings. – Andrea Corbellini Jan 29 '13 at 10:54
  • 2
    But this solution creates an extra shell / process. Can this be avoided? – lucasvc Sep 5 '16 at 10:26
  • 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. – Stefano Palazzo Sep 5 '16 at 12:04
  • 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. – Gabriel Staples Feb 11 at 22:51
  • I made this an answer, since it should stand on its own as an answer: askubuntu.com/questions/20330/…. – Gabriel Staples Feb 11 at 23:02
14

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.

| improve this answer | |
  • 1
    -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. – Gabriel Staples Feb 11 at 6:00
10

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.

| improve this answer | |
  • 1
    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. – hsandt May 23 '18 at 16:37
8

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

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

Ex:

bash --init-file <(echo './bla.sh')
| improve this answer | |
7

If you are using xterm use -hold.

| improve this answer | |
2

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.

| improve this answer | |
2
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

| improve this answer | |
1

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
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.