In Bash scripting, the “$?” variable contains the exit status of the last run command. When a command is executed in a shell script, it returns an exit status in a numerical value. Thus, it stands to be a tool to verify the execution status of a Linux command. It is beneficial to perform error handling or to determine the outcome of a previous command.
This article will describe the meaning of “$?” along with the practical implementation.
- Meaning of $? in a Shell Script
- How Does $? Work in Shell Script?
- Check Whether the Last Command Was Executed or Failed
- Check the Command Was Found or Not
Meaning of $? in a Shell Script
The value of $? is 0 if the last executed command is completed successfully without any errors. If there is an error or the command failed for some reason, $? contains a non-zero value that indicates the type of error or failure that occurred.
How Does $? Work in Shell Script?
Let’s compute the value of “$?” in the “script.sh” file, after executing the specific command in the terminal. Here are some examples of using the “$?” in a shell script:
Example 1: Check whether the Last Command Was Executed or Failed
An example is considered to execute the “ls” command by specifying the “/home/itslinuxfoss/Downloads/” path:
$ ls /home/itslinuxfoss/Downloads/
$ sudo nano script.sh
The example code of the script is provided below:
if [ $? -eq 0 ]; then
echo "The command was successful"
else
echo "The command failed"
fi
In this code, the “ls” command is utilized to list the contents of the “/home/itslinuxfoss/Downloads/” directory. Then, check the value of $? to determine if the code is executed or not. If “$?” equals 0, print a success message. Otherwise, print a failure message.
Save and close the script file. After that, acquire the “+x” permission to execute the script in the terminal:
$ sudo chmod +x script.sh
$ ./script.sh
The output shows that the command is successfully executed.
Example 2: Check the Command Was Found or Not
In this example, use the “which” command to check if the “gcc” compiler is installed on the system, redirect the output to “/dev/null”:
$ which gcc > /dev/null 2>&1
$ sudo nano script.sh
Now, let’s create a script to check if the command was found:
if [ $? -eq 0 ]; then
echo "The gcc compiler was found"
else
echo "The gcc compiler was not found"
fi
In the script, check the value of “$?” to determine if the “gcc” command is found. If $? equals 0, print a message that “The gcc compiler was found”. Otherwise, print a message that “The gcc compiler was not found”.
Save and close the script file. Furthermore, use the “chmod” command with “+x” permission to make the script executable and then execute it:
$ sudo chmod +x script.sh
$ ./script.sh
The output confirms that the “The gcc compiler was found”.
Conclusion
The shell script offers the “$?” variable that has the exit status of the previously run script in the terminal. After executing the command, it returns a numerical value that confirms whether the command is successfully executed or not.
This guide has briefly illustrated the meaning of $? in a shell script along with a few examples.