In this post, I will explain about the various Exit status and How to check Exit status of Linux Commands. This post will be helpful for those who are trying to write scripts and dealing with conditional statements.
On Linux systems, programs can pass a value to their parent process while terminating. This value is referred to as an exit code or exit status. So, any script or command executed will have an exit code. The exit status value varies from 0 to 255. Some of the commonly used exit status are as below.
|0||Successful execution of command|
|1||command fails because of an error during expansion or redirection, the exit status is greater than zero.|
|2||Incorrect command usage|
|126||Command found but not executable|
|127||command not found|
When is it useful?
It is very useful while dealing with scripts. So, you can use command exit status in the shell script to display an error message or take some sort of action based on the exit code.
How to check Exit status of Linux Commands?
Exit codes in command line
You can use $? to find out the exit status of a Linux command. Execute echo $? command to check the status of executed command as shown below.
admin@GeeksAlive:~$ ls Desktop Documents Downloads examples.desktop Music Pictures Public sample.txt share Templates Videos admin@GeeksAlive:~$ echo $? 0
Here we get exit status as zero which means the “ls” command executed successfully.
Exit codes in scripts
Sample Script 1:
#!/bin/bash which vim check_vim=$? if [ $check_vim -eq 0 ]; then echo -e "Package already installed" else sudo apt-get install vim fi
In the above example, the conditional if statement is checking the exit status of the command “which vim” before trying to install “vim” on your system.
I will explain the use of manually specifying exit codes in scripts with another example.
Sample Script 2:
#!/bin/bash touch /root/test.txt if [ $? -eq 0 ] then echo "Successfully created file" else echo "Could not create file" fi
Let’s execute this script. See the details below.
admin@GeeksAlive:~$ sh test_exit.sh touch: cannot touch '/root/test.txt': Permission denied Could not create file admin@GeeksAlive:~$ echo $? 0
Here, the script didn’t really create the file “test.txt“. However, you can see that the exit status is “0“. This is because of the execution of the “echo” statement. So, it would not be a good idea to pass a successful exit code to any other program executing this script.
How can we avoid this?
In such cases, we can add our own exit code to this script using exit command. See the detailed script below.
Sample Script 3:
#!/bin/bash touch /root/test.txt if [ $? -eq 0 ] then echo "Successfully created file" exit 0 else echo "Could not create file" exit 1 fi
Here, with the exit command, the script will exit with a successful message and 0 exit code if the touch command is successful. However, If the touch command fails, we will print a failure message and exit with a 1 value which indicates failure.
admin@GeeksAlive:~$ sh test_exit.sh touch: cannot touch '/root/test.txt': Permission denied Could not create file admin@GeeksAlive:~$ echo $? 1
Hope you have a better idea about the exit codes and how to use it in scripts now. If you have any queries, please leave a comment below.