Exit Status($?) variable in Linux
Last Updated :
16 Oct, 2024
In Linux, $? is a special variable that holds the last executed command's exit status or return value. It is commonly used to check whether the previous command was successful or encountered an error.
By convention:
- A return value of 0 indicates successful execution.
- Any non-zero value indicates an error or unsuccessful execution.
This variable is particularly useful in shell scripts, as it allows conditional execution based on the outcome of previous commands.
Syntax
The $? operator is automatically set after each command execution. To display its value:
echo $?
Basic Example
If the last command was executed successfully, $? will return 0:
$ echo $?
0
If the last command failed, $? will return a non-zero value.
Working with "$?" operator
1. Default Value
Its default value is 0 when the system starts and no command has been executed yet. Even if the last command has not been successfully executed and the system is restarted, we get its value as 0 when the following command is entered into the terminal.
echo $?
2. Return Status of the Last Executed Command
It returns the exit status of the last executed command. In the example mentioned below, There is no command as eccho in UNIX and hence the last process was not successfully executed. So $? stores a non-zero value which is the exit status of the last executed command.
eccho
echo $?

3. Checking File Existence with $?
In the example mentioned below, If the file exists (can be either directory or file), then the return value by the "ls" command will be 0 (i.e, the command has been successfully executed) else it will display a number which is non-zero. The number depends on the program.
Referring to the image below, consider that by default "file" doesn't exist then $? stores a return value of 2 (the command was not successfully executed) but once created using touch it displays 0 as the ls command returns 0 since the file exists.
ls file
echo $?
touch file
echo $?

4. Exit Status of True and False Commands
Also, when we enter simple true and false values in the terminal,it displays 0 as true does nothing but exits with a status code 0. But if we give false then 1 will get printed as false exits with status code 1.
true
echo $?
false
echo $?

Conclusion
The $? operator is a valuable tool for checking the status of the last executed command, especially in scripting. Understanding how to interpret its values allows you to create more robust and error-tolerant scripts, as decisions can be made based on whether the previous command was successful or not.
Similar Reads
Environment Variables in Linux/Unix Environment variables, often referred to as ENVs, are dynamic values that wield significant influence over the behavior of programs and processes in the Linux operating system. These variables serve as a means to convey essential information to software and shape how they interact with the environme
6 min read
Shell Scripting - Variable Substitution A shell is an interface that helps users to connect with the system. Using a shell is equivalent to indirectly communicating with the operating system. In Linux distributed systems, each time we use a terminal, we connect with a shell. The job of a shell is to analyze Unix commands or instructions g
3 min read
Set - $VARIABLE" in bash In the world of bash scripting, you may come across the phrase "set - $VARIABLE". But what does it mean? At its most basic, "set - $VARIABLE" is used to split the value of a bash variable into separate words, using the Internal Field Separator (IFS) as the delimiter. For example, if VARIABLE has the
3 min read
Shell Scripting - Shell Variables A shell variable is a character string in a shell that stores some value. It could be an integer, filename, string, or some shell command itself. Basically, it is a pointer to the actual data stored in memory. We have a few rules that have to be followed while writing variables in the script (which
6 min read
Bash Pathname Expansion in Linux When you type a command and press enter, bash performs several processes upon the text before carrying out our command as a command. The process that makes this happen is called expansion. For example, suppose you use the echo command for standard output. echo hi Output hi If you use echo with aster
3 min read