Friday, May 3, 2024
27
rated 0 times [  27] [ 0]  / answers: 1 / hits: 21607  / 3 Years ago, sun, june 13, 2021, 3:34:07

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?


More From » command-line

 Answers
2

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


[#19160] Sunday, June 13, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
irtuallyefu

Total Points: 429
Total Questions: 97
Total Answers: 119

Location: Hong Kong
Member since Tue, Oct 19, 2021
3 Years ago
irtuallyefu questions
;