Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does `echo $?` mean in bash? [duplicate]

I came upon the following command:

echo $?

what does that command do?

like image 682
Ivan Prodanov Avatar asked Dec 25 '22 21:12

Ivan Prodanov


1 Answers

Echoes (prints) the exit value for the previous command.

If it failed it will be different than zero (0).

$ cd ~
$ echo $?
> 0
$ cd //whatever/
> bash: cd: //whatever/: No such file or directory
$ echo $?
> 1

Programs exit with a status code. Every program is unique and has a different set of failure codes, but it's universally acknowledged that 0 is the 'success' code.

like image 117
Chris Pfohl Avatar answered Jan 08 '23 07:01

Chris Pfohl