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.

share|improve this question
    
Can you modify the script? – enzotib Jan 6 '11 at 17:57
    
yes, its my script. – UAdapter Jan 6 '11 at 18:07
    
@enzotib What if one cannot modify it, and the script ends with exit 0. Can whoever runs the script prevent the script from executing exit? – Geremia Aug 5 '17 at 16:47
    
@enzotib I answered my question. What I needed to do is run the script like sh myscript.sh instead of . myscript.sh. – Geremia Aug 5 '17 at 16:57
up vote 56 down vote accepted

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.

share|improve this answer
1  
it works great, thx. – UAdapter Jan 6 '11 at 20:33
16  
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
    
Nice, the exec $SHELL technique works well. – Ectropy Sep 25 '15 at 18:06
    
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

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.

share|improve this answer
    
Thanks! I used x-terminal-emulator -e "bash -c 'python3 /home/user/script.py';bash" to double click on bash script to opet terminal windows konsole and to calculate uuid in python, write that in terminal and leave terminal window open – Hrvoje T Feb 6 '17 at 23:51

If you are using xterm use -hold.

share|improve this answer
    
Yes, but the terminal is inert :-/ – Hibou57 Sep 9 '17 at 20:32

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

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

Ex:

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

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.

share|improve this answer
    
The question was about Ubuntu, not Windows. (Ubuntu-on-Windows wasn't around in 2011) – luk3yx Mar 30 '17 at 6:38
    
Ah sorry, this answer was supposed to be for another website. Is it better if I remove my answer or keep it here as a reference? – jaycode Mar 30 '17 at 8:31
    
Maybe remove the Windows 10 bit, your read answer is valid. – luk3yx Mar 30 '17 at 19:02
1  
Updated, thank you – jaycode Mar 30 '17 at 19:42

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.

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

share|improve this answer

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.