What does '$?' mean in bash scripts?

bash, syntax

Solution

$?

is the last error (or success) returned:

$?
1: command not found.
echo $?
127

false 
echo $?
1

true 
echo $?
0

The exit in the end:

exit $?

is superfluous, because the bash script will exit with that status anyway. Citing the man page:

Bash's exit status is the exit status of the last command executed in the script.

Problem

Possible Duplicate: What does “$?” give us exactly in a shell script? What does `$?` mean in a bash script? Example below: ``` #!/bin/bash # userlist.sh PASSWORD_FILE=/etc/passwd n=1 # User number for name in $(awk 'BEGIN{FS=":"}{print $1}' < "$PASSWORD_FILE" ) do echo "USER #$n = $name" let "n += 1" done exit $? ```

Original source

Related problems