'$' 是什么意思?在 bash 脚本中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10032078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:55:12  来源:igfitidea点击:

What does '$?' mean in bash scripts?

bashsyntax

提问by Meekohi

Possible Duplicate:
What does “$?” give us exactly in a shell script?

可能的重复:
什么是“$?” 给我们一个shell脚本?

What does $?mean in a bash script? Example below:

$?在 bash 脚本中是什么意思?下面的例子:

#!/bin/bash
# userlist.sh

PASSWORD_FILE=/etc/passwd
n=1           # User number

for name in $(awk 'BEGIN{FS=":"}{print }' < "$PASSWORD_FILE" )

do
  echo "USER #$n = $name"
  let "n += 1"
done

exit $?

回答by user unknown

$?

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 脚本无论如何都会以该状态退出。引用手册页:

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

Bash 的退出状态是脚本中执行的最后一个命令的退出状态。