You can use exec ./your-script
.
A terminal emulator like GNOME Terminal quits when the initial process running inside it--which is usually a shell--quits.
If you are already in a terminal and the only thing you want to do before quitting that terminal is to run a particular script (or program), then this means you no longer really need the shell that's running in it anymore. Thus you can use the shell's exec
builtin to make the shell replace itself with the process created by your command.
- In the case of your script, that's another shell process--just as how a second shell process is created when you run a script without
exec
.
The syntax is exec command
, e.g., exec ./your-script
.
exec
: an Example
For example, suppose I have a shell script called count
, marked executable, and located in the current directory. It contains:
#!/usr/bin/env bash
for i in {5..1}; do echo $i; sleep 1; done
And, in a terminal, I run:
exec ./count
This prints the numerals 5
, 4
, 3
, 2
, and 1
, one per second, and then the terminal window closes.
If you run this from something other than the first process run in your terminal--for example, if you ran bash
first to start another shell instance--then this brings you back to the shell that created that process, rather than quitting the terminal. (This caveat applies equally to exit
-based methods.)
You can use ./your-script; exit
.
If you don't want to tell your shell to replace itself with the new process (via exec
), you can tell it to stick around but quit itself immediately after the new process finishes.
To do this, run your command and the exit
command, separated by ;
so they can be given on one line.
The syntax is command; exit
, e.g., ./your-script; exit
.
command; exit
vs. command && exit
You may notice this looks similar to the ./your-script && exit
method suggested in kos's and heemayl's answers. The difference is that:
&&
runs the second command only if the first command reported that it succeeded by returning an exit code of zero.
;
runs the second command regardless of whether or not the first command reported success.
Which one you want depends on the specific situation. If the command fails, do you want the calling shell (and hosting terminal) to stay up? If so, use &&
; if not, use ;
.
There is also command || exit
, which quits the calling shell only if command
reported failure.
gnome-open
withxdg-open
in my script but there is no change. The terminal still remains open