Exiting with a Return Code
The exit
command terminates your script and passes a
given return code to the shell. Return codes are the reason that commands could be run in
sequence in Combining commands: the shell checks the return code of the
preceding command before running the next. Also, a shell script that calls another script
can check its exit code in conditional statements to determine what to do next.
By tradition, scripts should return 0 for success and 1 (or other nonzero value) on
failure. If your script doesn’t call exit
, the return
code is automatically 0:
if [ $# -lt 2 ] then echo "$0 error: you must supply two arguments" exit 1 else echo "My name is $1 and I come from $2" fi exit 0 ➜./myscript Bob
./myscript error: you must supply two arguments ➜echo $?
1
Get Macintosh Terminal Pocket Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.