68

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.

77

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.

  • 20
    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
  • 1
    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
10

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.

7

If you are using xterm use -hold.

7

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.

  • 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
4

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

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

Ex:

bash --init-file <(echo './bla.sh')
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.

1
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

protected by Radu Rădeanu Sep 30 '14 at 8:53

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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