If you are executing a Bash script in your terminal and need to stop it before it exits on its own, you can use the Ctrl + C combination on your keyboard. A ^C character will appear in your terminal to indicate a keyboard interrupt.
To exit from bash type exit and press ENTER . If your shell prompt is > you may have typed ' or " , to specify a string, as part of a shell command but have not typed another ' or " to close the string. To interrupt the current command press CTRL-C .
Often when writing Bash scripts, you will need to terminate the script when a certain condition is met or to take action based on the exit code of a command.
exit exits the calling shell or shell script with the exit status specified by n . If you omit n , the exit status is that of the last command executed (an end-of-file exits the shell). return exits a function with the return value specified by n . If you omit n , the return status is that of the last command executed.
The "problem" really is that you're sourcing and not executing the script. When you source a file, its contents will be executed in the current shell, instead of spawning a subshell. So everything, including exit, will affect the current shell.
Instead of using exit
, you will want to use return
.
Yes; you can use return
instead of exit
. Its main purpose is to return from a shell function, but if you use it within a source
-d script, it returns from that script.
As §4.1 "Bourne Shell Builtins" of the Bash Reference Manual puts it:
return [n]
Cause a shell function to exit with the return value n. If n is not supplied, the return value is the exit status of the last command executed in the function. This may also be used to terminate execution of a script being executed with the
.
(orsource
) builtin, returning either n or the exit status of the last command executed within the script as the exit status of the script. Any command associated with theRETURN
trap is executed before execution resumes after the function or script. The return status is non-zero ifreturn
is used outside a function and not during the execution of a script by.
orsource
.
You can add an extra exit command after the return statement/command so that it works for both, executing the script from the command line and sourcing from the terminal.
Example exit code in the script:
if [ $# -lt 2 ]; then
echo "Needs at least two arguments"
return 1 2>/dev/null
exit 1
fi
The line with the exit
command will not be called when you source the script after the return
command.
When you execute the script, return
command gives an error. So, we suppress the error message by forwarding it to /dev/null
.
Instead of running the script using . run2.sh
, you can run it using sh run2.sh
or bash run2.sh
A new sub-shell will be started, to run the script then, it will be closed at the end of the script leaving the other shell opened.
Actually, I think you might be confused by how you should run a script
.
If you use sh
to run a script, say, sh ./run2.sh
, even if the embedded script ends with exit
, your terminal window will still remain.
However if you use .
or source
, your terminal window will exit/close as well when subscript ends.
for more detail, please refer to What is the difference between using sh
and source
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With