bash $做什么?$0 $1 $2 在 shell 脚本中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29258603/
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-08 21:54:11  来源:igfitidea点击:

What do $? $0 $1 $2 mean in shell script?

bashshellash

提问by Lin

I often come across $? $0 $1 $2 etc....in shell scripting, what I know is that $?returns the exit status of the last command

我经常$? $0 $1 $2 etc....在shell脚本中遇到,我所知道的是$?返回最后一个命令的退出状态

echo "this will return 0"
echo $?

but what do the others do? what are they called and is there more? perhaps like $3 $4 $5 ...

但是其他人在做什么?他们叫什么,还有更多吗?也许像 3 美元 4 美元 5 美元……

回答by Grzegorz ?ur

These are positional arguments of the script.

这些是脚本的位置参数。

Executing

执行

./script.sh Hello World

Will make

会使

##代码## = ./script.sh
 = Hello
 = World

Note

笔记

If you execute ./script.sh, $0will give output ./script.shbut if you execute it with bash script.shit will give output script.sh.

如果你执行./script.sh$0会给出输出,./script.sh但如果你用bash script.sh它执行它会给出输出script.sh

回答by Etan Reisner

They are called the Positional Parameters.

它们被称为位置参数

3.4.1 Positional Parameters

A positional parameter is a parameter denoted by one or more digits, other than the single digit 0. Positional parameters are assigned from the shell's arguments when it is invoked, and may be reassigned using the set builtin command. Positional parameter N may be referenced as ${N}, or as $N when N consists of a single digit. Positional parameters may not be assigned to with assignment statements. The set and shift builtins are used to set and unset them (see Shell Builtin Commands). The positional parameters are temporarily replaced when a shell function is executed (see Shell Functions).

When a positional parameter consisting of more than a single digit is expanded, it must be enclosed in braces.

3.4.1 位置参数

位置参数是由一位或多位数字表示的参数,而不是单个数字 0。位置参数是在调用时从 shell 的参数分配的,并且可以使用 set 内置命令重新分配。位置参数 N 可以引用为 ${N},或者当 N 由单个数字组成时引用为 $N。位置参数不能用赋值语句赋值。set 和 shift 内置函数用于设置和取消设置它们(请参阅 Shell 内置命令)。执行 shell 函数时,位置参数会被临时替换(请参阅 Shell 函数)。

当扩展包含多个数字的位置参数时,必须将其括在大括号中。