TLDR;
Use exec bash;
or exec $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 the bash
executable is located in /usr/bin/bash
, however. BUT, /user/bin/bash
and /bin/bash
are identical executables, as proven by having the same sha256sum
:
$ which bash
/usr/bin/bash
$ sha256sum $(which bash)
04a484f27a4b485b28451923605d9b528453d6c098a5a5112bec859fb5f2eea9 /usr/bin/bash
$ sha256sum $SHELL
04a484f27a4b485b28451923605d9b528453d6c098a5a5112bec859fb5f2eea9 /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.
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 already cd
ed into your ~/some_dir
directory:
Add to the bottom of your ~/.bashrc
file:
open_default_tabs() {
gnome-terminal --tab -- bash -ic "cd ~/some_dir; exec bash;"
gnome-terminal --tab -- bash -ic "cd ~/some_dir; exec bash;"
}
OR: again, you can use exec $SHELL
here instead as well:
open_default_tabs() {
gnome-terminal --tab -- bash -ic "cd ~/some_dir; exec $SHELL;"
gnome-terminal --tab -- bash -ic "cd ~/some_dir; exec $SHELL;"
}
Then in any gnome-terminal already open, re-source your ~/.bashrc
file with . ~/.bashrc
, then run this command with:
open_default_tabs
...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;
or exec $SHELL;
portion from the end of each of the commands above, however, then re-source your ~/.bashrc
file with . ~/.bashrc
, then run the open_default_tabs
command again, you'll see that the tabs flicker open then immediately close. All you'll see is a couple quick flashes. The exec bash;
or exec $SHELL;
part at the end is required to keep these new tabs open.
Test 2: this proves that exec bash;
works. BUT, the behavior regarding gnome-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
:
gnome-terminal --tab -- bash -ic "cd ~/some_dir; exec bash;"
gnome-terminal --tab -- bash -ic "cd ~/some_dir; exec bash;"
Ensure some_dir
exists, and make test.sh
executable:
mkdir -p ~/some_dir
chmod +x test.sh
Run it:
./test.sh
Notice the two windows (not tabs, in this case, due to a problem in gnome-terminal
) which were just opened stay open. Delete exec bash;
above and you'll see they don't--they instantly close--so fast you won't even see them open. This proves exec bash;
is working perfectly. The gnome-terminal
behavior isn't quite as expected, but that has nothing to do with exec bash;
, which works just fine. Again, see my other answer here for a full work-around to the problem specific to gnome-terminal
in this case.
Related:
- Here's why I REALLY needed this information! This is my "other answer" I mention above: Open Terminal with multiple tabs and execute application