Ubuntu – `$?` ? Is it a variable

bashcommand line

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?

Best Answer

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