What Does $? Do in Linux?
Contents

The $? variable represents the exit status of the previous command. Exit status is a numerical value returned by every command upon its completion. As a rule, most commands return an exit status of 0 if they were successful, and 1 if they were unsuccessful.

What is $? In Shell?

$? is a special variable in shell that 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.

What does $? Do in bash?

In Bash, $? expression prints the status of the last command executed. ~$ echo $? Our last command was test 5 -gt 9 and it exited with status 1 which means the expression 5 -gt 9 is false .

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest