bash 术语之间的区别:“选项”、“参数”和“参数”?

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

Difference between terms: "option", "argument", and "parameter"?

bashshellparametersargumentsoption

提问by what is what

What are the differences between these terms: "option", "argument", and "parameter"? In man pages these terms often seem to be used interchangeably.

这些术语之间有什么区别:“选项”、“参数”和“参数”?在手册页中,这些术语似乎经常互换使用。

回答by jlliagre

A commandis split into an array of strings named arguments. Argument 0 is (normally) the command name, argument 1, the first element following the command, and so on. These arguments are sometimes called positional parameters.

命令被分成称为字符串数组参数。参数 0(通常)是命令名称、参数 1、命令后面的第一个元素,依此类推。这些参数有时称为位置参数

$ ls -la /tmp /var/tmp
arg0 = ls
arg1 = -la
arg2 = /tmp
arg3 = /var/tmp

An optionis a documented type of argumentmodifying the behavior of a command, e.g. -lcommonly means "long", -vverbose. -lvare twooptions combined in a singleargument. There are also long optionslike --verbose(see also Using getopts to process long and short command line options). As their name suggests, options are usually optional. There are however some commands with paradoxical "mandatory options".

一个选项是一个记录类型参数修改的行为命令,例如,-l通常的意思是“长”,-v冗长。-lv2级的选项合并于一个单一的参数。还有一些长选项,例如--verbose(另请参阅使用 getopts 处理长和短命令行选项)。顾名思义,选项通常是可选的。然而,有些命令具有矛盾的“强制选项”。

$ ls -la /tmp /var/tmp
option1= -l
option2= -a

A parameteris an argument that provides information to either the commandor one of its options, e.g. in -o file, fileis the parameter of the -ooption. Unlike options, whose possible values are hard coded in programs, parameters are usually not, so the user is free to use whatever string suits his/her needs. Should you need to pass a parameter that looks like an option but shouldn't be interpreted as such, you can separate it from the beginning of the command line with a double dash: --.

参数是要么所述提供信息的参数命令或它的一个选项,例如在-o file文件是的参数-o选项。与选项的可能值在程序中硬编码不同,参数通常不是,因此用户可以自由使用任何适合他/她需要的字符串。如果您需要传递一个看起来像选项但不应被解释为选项的参数,您可以使用双破折号将其与命令行的开头分开:--

$ ls -la /tmp /var/tmp
parameter1= /tmp
parameter2= /var/tmp

$ ls -l -- -a
option1    = -l
parameter1 = -a

A shell parameteris anything that store a value in the context of the shell. This includes positional parameters (e.g. $1, $2...), variables (e.g. $foo, $bar...) and special character ones (e.g. $@)

一个shell参数是任何存储在外壳的背景值。这包括位置参数(例如$1$2...),变量(例如$foo$bar...)和特殊字符(例如$@

Finally, there are subcommands, also known as functions / (low-level) commands, which are used with "metacommands" that embed multiple separate commands, like busybox, git, apt-get, openssl, and the likes. With them, you might have global options preceeding the subcommand, and subcommand specific options that follow the subcommand. Unlike parameters, the list of possible subcommands is hardcoded in the command itself. e.g.:

最后,还有子命令,也称为函数/(低级)命令,它们与嵌入多个单独命令的“元命令”一起使用,例如busyboxgitapt-getopenssl等。使用它们,您可能在子命令之前有全局选项,在子命令之后可能有子命令特定选项。与参数不同,可能的子命令列表在命令本身中进行了硬编码。例如:

$ busybox ls -l
command            = busybox
subcommand         = ls
subcommand option1 = -l

$ git --git-dir=a.git --work-tree=b -C c status -s
command            = git
command option1    = --git-dir=a.git
command option2    = --work-tree=b
command option3    = -C
subcommand         = status
subcommand option1 = -s

Note that some commands like test, tar, ddand findhave more complex argument parsing syntax than the ones described previously and can have some or all of their arguments parsed as expressions, operands, keysand similar command specific components.

请注意,某些命令喜欢testtardd并且find具有比那些先前所描述的,可以有部分或全部它们的参数解析为更复杂的参数解析语法expressionsoperandskeys和类似命令的具体内容。

Note also that optional variable assignments and redirections, despite being processed by the shell for tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal like other command line parameters are not taken into account in my reply because they have disappeared when the command is actually called and passed its arguments.

另请注意,可选变量分配和重定向,尽管由 shell 处理以进行波浪号扩展、参数扩展、命令替换、算术扩展和引号删除等其他命令行参数,但在我的回复中并未考虑在内,因为它们在命令实际上被调用并传递了它的参数。

回答by starfry

The manpage for a typical Unix command often uses the terms argument, optionand parameter. At the lowest level, we have argumentand everything is an argument, including the (filesystem path to the) command itself.

man典型 Unix 命令的页面通常使用术语argument,optionparameter。在最低级别,我们有argument并且一切都是一个参数,包括(文件系统路径)命令本身。

In a shell script you access arguments using the special variables $0.. $n. Other languages have similar ways to access them (commonly through an array with a name like argv).

在 shell 脚本中,您可以使用特殊变量$0..访问参数$n。其他语言也有类似的方式来访问它们(通常通过一个名字像 的数组argv)。

Arguments may be interpreted as optionsif you wish. How this is done is implementation-specific. You can either roll your own, for exampe a shell (such as bash) script can use provided getoptsor getoptcommands.

如果您愿意,可以将参数解释为选项。如何做到这一点是特定于实现的。您可以使用自己的,例如 shell(例如bash)脚本可以使用提供的getoptsgetopt命令。

These typically define an optionas an argument beginning with a hyphen (-) and some options may use proceeding arguments as its parameters. More capable parsers (e.g getopt) support mixing short-form (-h) and long-form (--help) options.

这些通常将选项定义为以连字符 ( -)开头的参数,并且某些选项可能使用后续参数作为其参数。更强大的解析器(例如getopt)支持混合短格式 ( -h) 和长格式 ( --help) 选项。

Typically, most options take zero or one parameter. Such parameters are also sometimes called values.

通常,大多数选项采用零个或一个参数。此类参数有时也称为

The supported options are coded in the program code (e.g in the invocation of getoptswithin a shell script). Any remaining arguments after the options have been consumed are commonly called positional parameterswhen the order in which they are given is significant (this is in contrast to options which usually can be given in any order).

支持的选项编码在程序代码中(例如,getopts在 shell 脚本中调用)。选项被消耗后的任何剩余参数通常称为位置参数,当它们给出的顺序很重要时(这与通常可以以任何顺序给出的选项相反)。

Again, the script defines what the positional parameters are by how it consumes and uses them.

同样,脚本通过它如何消耗和使用位置参数来定义位置参数是什么。

So a typical command

所以一个典型的命令

$ ls -I README -l foo 'bar car' baz

has seven arguments: /usr/bin/ls, -I, README, -l, foo, bar car, and bazaccessible as $0thru $6. The -land -Iare interpreted as options, the latter having a parameter(or value) of README. What remains are positional parameters(foo, bar carand baz).

有七个参数:/usr/bin/ls, -I, README, -l, foo, bar car, 并且baz可以$0通过访问$6。的-l-I被解释为选项,后者具有一个参数(或的)README。剩下的是位置参数foo,bar carbaz)。

Option parsing may alter the argument list by removing those it consumes (e.g using shiftor set) so that only the positional parameters remain and are thereafter accessible as $1.. $n.

选项解析可以通过删除它消耗的那些参数(例如使用shiftset)来改变参数列表,以便只保留位置参数并且此后可以作为$1..访问$n

回答by joelostblom

Since the question is tagged "bash", I looked for relevant sections in the Bash manual. I list these as quoted passages below together with my own one sentence summaries.

由于问题被标记为“bash”,我在Bash 手册中查找了相关部分。我将这些与我自己的一句话摘要一起列为下面的引用段落。

Arguments

参数

Everything following the command is an argument.

命令后面的所有内容都是参数。

A simple shell command such as echo a b cconsists of the command itself followed by arguments, separated by spaces.

A simple command is the kind of command encountered most often. It's just a sequence of words separated by blanks, terminated by one of the shell's control operators (see Definitions). The first word generally specifies a command to be executed, with the rest of the words being that command's arguments.

一个简单的 shell 命令,例如echo a b c由命令本身后跟参数组成,以空格分隔。

一个简单的命令是最常遇到的那种命令。它只是由空格分隔的一系列单词,由 shell 的一个控制运算符终止(请参阅定义)。第一个单词通常指定要执行的命令,其余单词是该命令的参数。

Parameters

参数

Arguments are referred to as parameters during function execution.

参数在函数执行期间被称为参数。

When a function is executed, the arguments to the function become the positional parameters during its execution

A parameter is an entity that stores values. It can be a name, a number, or one of the special characters listed below. A variable is a parameter denoted by a name.

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.

当一个函数被执行时,该函数的参数在其执行过程中变成了位置参数

参数是存储值的实体。它可以是名称、数字或下面列出的特殊字符之一。变量是由名称表示的参数。

位置参数是由一位或多位数字表示的参数,而不是单个数字 0。位置参数是在调用时从 shell 的参数分配的,并且可以使用 set 内置命令重新分配。位置参数 N 可以引用为 ${N},或者当 N 由单个数字组成时引用为 $N。

Options

选项

There is no dedicated section to defining what an option is, but they are referred to as hyphen-prefixed characters throughout the manual.

没有专门的部分来定义选项是什么,但在整个手册中它们被称为以连字符为前缀的字符。

The -p option changes the output format to that specified by POSIX

-p 选项将输出格式更改为 POSIX 指定的格式