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:A small flaw: since
gnome-terminal
isn't running thebash
as it's shell, it will regard it as an application and display a warning about it when you try to close the terminal:I've found no nice way to hide this warning. If you want, you can disable it entirely by running:
This doesn't happen if you're using
xterm
instead of gnome-terminal; should it bother you.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:
This does the following:
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.
If you have access to the script, you may also add the following code at the end:
That code will wait for an input before closing, so the terminal will stay open until you press Enter.
Use
bash
's--init-file
option with a temporary pipe:Ex:
If you are using
xterm
use-hold
.Using
xterm
and appending;bash
at the end of the command string works. For example:This does the following:
script.sh
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.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 ranbla.sh
instead. This can be remedied by puttingat the top of
bla.sh
TLDR;
Use
exec bash;
orexec $SHELL;
at the end of a command to keep the terminal open after running the command.Details and sample test code
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 (this was last tested and checked on Ubuntu 20.04),
echo $SHELL
shows that the SHELL variable contains/bin/bash
.which bash
shows that thebash
executable is located in/usr/bin/bash
, however. BUT,/user/bin/bash
and/bin/bash
are identical executables, as proven by having the samesha256sum
: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 isexec bash
orexec $SHELL
, as @AndreaCorbellini says in his comment.Test 1: this will work as expected
Here's what this might look like in a use-case of having a script to open up two new
gnome-terminal
tabs alreadycd
ed into your~/some_dir
directory:Add to the bottom of your
~/.bashrc
file:OR: again, you can use
exec $SHELL
here instead as well:Then in any gnome-terminal already open, re-source your
~/.bashrc
file with. ~/.bashrc
, then run this command with:...and you'll see your original gnome-terminal tab plus two new tabs, with both of the two new tabs already
cd
ed into the~/some_dir
directory.If you delete the
exec bash;
orexec $SHELL;
portion from the end of each of the commands above, however, then re-source your~/.bashrc
file with. ~/.bashrc
, then run theopen_default_tabs
command again, you'll see that the tabs flicker open then immediately close. All you'll see is a couple quick flashes. Theexec bash;
orexec $SHELL;
part at the end is required to keep these new tabs open.Test 2: this proves that
exec bash;
works. BUT, the behavior regardinggnome-terminal
isn't quite as expected. To solve that, see my more-complicated answer and work-around in my other answer here.In
test.sh
:Ensure
some_dir
exists, and maketest.sh
executable:Run it:
Notice the two windows (not tabs, in this case, due to a problem in
gnome-terminal
) which were just opened stay open. Deleteexec bash;
above and you'll see they don't--they instantly close--so fast you won't even see them open. This provesexec bash;
is working perfectly. Thegnome-terminal
behavior isn't quite as expected, but that has nothing to do withexec bash;
, which works just fine. Again, see my other answer here for a full work-around to the problem specific tognome-terminal
in this case.Related: