shellscriptingspecial-characters

Meaning of $? (dollar question mark) in shell scripts


What does

echo $?

mean in shell programming?


Solution

  • This is the exit status of the last executed command.

    For example the command true always returns a status of 0 and false always returns a status of 1:

    true
    echo $? # echoes 0
    false
    echo $? # echoes 1
    

    From the manual: (acessible by calling man bash in your shell)

    ?       Expands to the exit status of the most recently executed foreground pipeline.

    By convention an exit status of 0 means success, and non-zero return status means failure. Learn more about exit statuses on wikipedia.

    There are other special variables like this, as you can see on this online manual: https://www.gnu.org/s/bash/manual/bash.html#Special-Parameters