From man bash
:
$? Expands to the exit status of the most recently executed foreground pipeline.
echo $?
will return the exit status of last command. You got 127
that is the exit status of last executed command exited with some error (most probably). Commands on successful completion exit with an exit status of 0
(most probably). The last command gave output 0
since the echo $v
on the line previous finished without an error.
If you execute the commands
v=4
echo $v
echo $?
You will get output as:
4 (from echo $v)
0 (from echo $?)
Also try:
true
echo $?
You will get 0
.
false
echo $?
You will get 1
.
The true
command does nothing, it just exits with a status code 0
; and the false
command also does nothing, it just exits with a status code indicating failure (i.e. with status code 1
).