101

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.

UAdapter
  • 17,967

9 Answers9

118

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.

19

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.

nanofarad
  • 20,906
Karsvo
  • 199
19

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.

wjandrea
  • 14,504
jaycode
  • 322
  • 2
  • 7
10

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

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

Ex:

bash --init-file <(echo './bla.sh')
muru
  • 207,228
8

If you are using xterm use -hold.

Eliah Kagan
  • 119,640
Ramy
  • 89
3

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 cded 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 cded 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:

  1. Here's why I REALLY needed this information! This is my "other answer" I mention above: Open Terminal with multiple tabs and execute application
Gabriel Staples
  • 11,502
  • 14
  • 97
  • 142
3

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.

Eliah Kagan
  • 119,640
Karsvo
  • 39
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

Zombo
  • 1
0

I have found out that gnome-terminal is a bit bad on doing this, xterm is more serene to do this task without poping up questions. For instance, I executed a command "date" and it was COVERED BY A BLUE POPUP from gnome-terminal saying that the child process exit fine, impeding me to see the output of date.

Gnome-terminal should add a menu "Disable all terminal messages"

IMPORTANT: you should be in the execution directory where all dependences are found from '.'

Type this, and the terminal will be kept in a new process:

xterm -fa Monospace -fs 10 -e "bash -c 'mycommand arg1 arg2 argn';bash" &