What is `$?` ? Is it a variable?

The question is pretty straight forward. I can get the exit code of the previous command by using $?. But what exactly is it?

I don’t think it is a variable, because you can’t have any special characters in the name of variable other than _.

foo?foo=10

will result in the error: foo?foo=10: command not found

So if it is not a variable, what is it? Are there others like it?

Asked By: daltonfury42

||

$? is a special variable in bash that always holds the return/exit code of the last executed command.

You can view it in a terminal by running echo $?.

Return codes are in the range [0; 255].
A return code of 0 usually means everything is ok.
Other values indicate errors. Most applications return 1 or custom values.
If a command was not found, the variable usually contains 127

If you want to set this variable to a specific value, you can easily do this with bash -c "exit 5" to return for example the return code 5.

Answered By: Byte Commander

What is it?

$? is a built-in variable that stores the exit status of a command, function, or the script itself.

$? reads the exit status of the last command executed. After a function returns, $? gives the exit status of the last command executed in the function. This is Bash’s way of giving functions a “return value.” It returns 0 on success or an integer in the range 1 - 255 on error.

Are there others like it too?

Yes,there are several such built-in variables in bash. You can see a list here.
Refer:
http://www.tldp.org/LDP/abs/html/exit-status.html

Answered By: Ron

$? is a variable but a special one, that’s why special characters are allowed.
($?) Expands to the exit status of the most recently executed foreground pipeline.

It’s not the only one, the shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed :

Variable    Meaning
$0          Filename of script
$1          Positional parameter #1
$2 - $9     Positional parameters #2 - #9
${10}       Positional parameter #10
$#          Number of positional parameters
"$*"        All the positional parameters (as a single word) *
"$@"        All the positional parameters (as separate strings)
${#*}       Number of positional parameters
${#@}       Number of positional parameters
$?          Return value
$$          Process ID (PID) of script
$-          Flags passed to script (using set)
$_          Last argument of previous command
$!          Process ID (PID) of last job run in background

* Must be quoted, otherwise it defaults to $@.

Sources:

Answered By: Sylvain Pineau

$? is a special shell parameter (variable).

In general all user defined variables must be named by using only the characters from character class [:alnum:] i.e. [a-zA-Z0-9_] (also can’t begin with [0-9]).

In a nutshell, $? is a special variable (and there are quite a few of these) that can be considered as internal feature of the shell itself that provides the exit code of the previous command (whether succeded or failed).

You can read this to get more idea on variables.

Answered By: heemayl
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.