5

I came upon the following command:

echo $?

what does that command do?

NAND
  • 663
  • 8
  • 22
Ivan Prodanov
  • 34,634
  • 78
  • 176
  • 248
  • 11
    "RTFM" is and always has been a terrible answer. If someone asks a question, answer it or ignore it. Don't tell them they're stupid for not looking elsewhere. Its useless at best and rude at worst. As it happens, identifying these short little language-embedded tokens is challenging for a newcomer to the language. Which section of TFM should he look in? That would at least be helpful. – Stabledog Aug 30 '13 at 15:41
  • I'm going to put that on a tshirt! – James Geddes Aug 03 '21 at 06:38

1 Answers1

11

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.

Chris Pfohl
  • 18,220
  • 9
  • 68
  • 111