Search code examples
bashexitstatus

Bash $? Variable Assignement


I just started learning bash and I was reading about the $? variable. From what I understood, $? is assigned to the exit status of the last command executed.

For example

$ false; echo $?

Will yield 1, and

$ false; :; echo $?

Will yield 0

Things get a bit more complicated when I combine these with if/for blocks. The man page reads:

for NAME [in WORDS ... ] ; do COMMANDS; done

Execute commands for each member in a list.

The for loop executes a sequence of commands for each member in a list of items. If in WORDS ...; is not present, then in "$@" is assumed. For each element in WORDS, NAME is set to that element, and the COMMANDS are executed.

Exit Status:

Returns the status of the last command executed.

That means:

$ false; for i in 1 2 3; do false; done; echo $?

Will yield 1 since the last command executed is false. But

$ false; for i in; do false; done; echo $?

or

$ false; if false; then :; fi; echo $?

Will yield 0 despite the fact that false was the last command executed.

My guess is the $? variable is set to 0 when entering a for/if block. Am I missing something?


Solution

  • According to bash's manual:

    if list ; then list ; [ elif list ; then list ; ] ... [ else list ; ] fi

    The iflist is executed. If its exit status is zero, the thenlist is executed. Otherwise, each eliflist is executed in turn, and if its exit status is zero, the corresponding thenlist is executed and the command completes. Otherwise, the elselist is executed, if present. The exit status (of the whole if ... fi block) is the exit status of the last command (in the then, elif or else block) executed, or zero if no condition tested true.