18

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 $?
Community
  • 1
  • 1
Meekohi
  • 10,390
  • 6
  • 49
  • 58

1 Answers1

21
$?

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.

Steve Koch
  • 912
  • 8
  • 21
user unknown
  • 35,537
  • 11
  • 75
  • 121