Bash Scripting – Exiting from Script
So far in our sample scripts, we terminated things pretty abruptly. When we were finished with our last command, we just ended the script. There’s a more elegant way of completing things available to us.
Every command that runs in the shell uses an exit status to indicate to the shell that it’s finished processing. The exit status is an integer value between 0 and 255 that’s passed by the command to the shell when the command finishes running. You can capture this value and use it in your scripts.
Checking the exit status
Linux provides the $? special variable that holds the exit status value from the last command that executed. You must view or use the $? variable immediately after the command you want to check. It changes values to the exit status of the last command executed by the shell:
$ date
Sat Jan 15 10:01:30 EDT 2014
$ echo $?
0
$
By convention, the exit status of a command that successfully completes is zero. If a command completes with an error, then a positive integer value is placed in the exit status:
$ asdfg
-bash: asdfg: command not found
$ echo $?
127
$
The invalid command returns an exit status of 127. There’s not much of a standard convention to Linux error exit status codes. However, you can use the guidelines shown in below table
An exit status value of 126 indicates that the user didn’t have the proper permissions set to execute the command:
$ ./myprog.c
-bash: ./myprog.c: Permission denied
$ echo $?
126
$
Another common error you’ll encounter occurs if you supply an invalid parameter to a command:
$ date %t
date: invalid date â%tâ
$ echo $?
1
$
This generates the general exit status code of 1, indicating that an unknown error occurred in the command.
The exit command
By default, your shell script exits with the exit status of the last command in your script:
$ ./test6
The result is 2
$ echo $?
0
$
You can change that to return your own exit status code. The exit command allows you to specify an exit status when your script ends:
$ cat test13
#!/bin/bash
# testing the exit status
var1=10
var2=30
var3=$[$var1 + $var2]
echo The answer is $var3
exit 5
$
When you check the exit status of the script, you get the value used as the parameter of the exit command:
$ chmod u+x test13
$ ./test13
The answer is 40
$ echo $?
5
$
You can also use variables in the exit command parameter:
$ cat test14
#!/bin/bash
# testing the exit status
var1=10
var2=30
var3=$[$var1 + $var2]
exit $var3
$
When you run this command, it produces the following exit status:
$ chmod u+x test14
$ ./test14
$ echo $?
40
$
You should be careful with this feature, however, because the exit status codes can only go up to 255. Watch what happens in this example:
$ cat test14b
#!/bin/bash
# testing the exit status
var1=10
var2=30
var3=$[$var1 * $var2]
echo The value is $var3
exit $var3
$
Now when you run it, you get the following:
$ ./test14b
The value is 300
$ echo $?
44
$
The exit status code is reduced to fit in the 0 to 255 range. The shell does this by using modulo arithmetic. The modulo of a value is the remainder after a division. The resulting number is the remainder of the specified number divided by 256. In the case of 300 (the result value), the remainder is 44, which is what appears as the exit status code.
Nicely written and explained.
Thank you.