bash 中命令行参数的 $opt 变量是什么
What is $opt variable for command line parameter in bash
我有下面的 bash 脚本,我对 $opt 的含义有点困惑。搜索后我没有找到有关它的详细信息。
test.sh:
echo "opt:" $opt
for opt; do
echo $opt
done
输出:
./test.sh -a -b c
opt:
-a
-b
c
for
语句通常是这样的:
for x in a b c; do
...
done
$x
在第一次迭代中的值为 a
,然后是 b
,然后是 c
。
a b c
部分不需要按字面意思写出来,要迭代的列表可以由变量给出,例如 $@
,它包含当前范围的所有参数列表:
for x in "$@"; do
...
done
此外,如果您没有明确提供列表,in "$@"
是 假定的:
for x; do
...
done
for name [ [ in [ word ... ] ] ; ] do list ; done
The list of words following in is expanded, generating a list of
items. The variable name is set to each element of this list in
turn, and list is executed each time. If the in word is omit‐
ted, the for command executes list once for each positional
parameter that is set (see PARAMETERS below). The return status
is the exit status of the last command that executes. If the
expansion of the items following in results in an empty list, no
commands are executed, and the return status is 0.
因此,如果缺少 in
单词,for 循环将遍历脚本的位置参数,即 </code>、<code>
、</code>、.. ..</p>
<p>名字没有什么特别的<code>opt
,任何合法的变量名都可以。考虑这个脚本及其输出:
#test.sh
echo $* # output all positional parameters , ,...
for x
do
echo $x
done
$ ./test.sh -a -b hello
-a -b hello
-a
-b
hello
$opt
不是内置变量或特殊变量。如果在脚本中提到它(在 for opt
之外没有 in something
部分,) it should either be defined earlier in the script or it's expected that it is export
ed 在 运行 脚本之前。
我有下面的 bash 脚本,我对 $opt 的含义有点困惑。搜索后我没有找到有关它的详细信息。
test.sh:
echo "opt:" $opt
for opt; do
echo $opt
done
输出:
./test.sh -a -b c
opt:
-a
-b
c
for
语句通常是这样的:
for x in a b c; do
...
done
$x
在第一次迭代中的值为 a
,然后是 b
,然后是 c
。
a b c
部分不需要按字面意思写出来,要迭代的列表可以由变量给出,例如 $@
,它包含当前范围的所有参数列表:
for x in "$@"; do
...
done
此外,如果您没有明确提供列表,in "$@"
是 假定的:
for x; do
...
done
for name [ [ in [ word ... ] ] ; ] do list ; done The list of words following in is expanded, generating a list of items. The variable name is set to each element of this list in turn, and list is executed each time. If the in word is omit‐ ted, the for command executes list once for each positional parameter that is set (see PARAMETERS below). The return status is the exit status of the last command that executes. If the expansion of the items following in results in an empty list, no commands are executed, and the return status is 0.
因此,如果缺少 in
单词,for 循环将遍历脚本的位置参数,即 </code>、<code>
、</code>、.. ..</p>
<p>名字没有什么特别的<code>opt
,任何合法的变量名都可以。考虑这个脚本及其输出:
#test.sh
echo $* # output all positional parameters , ,...
for x
do
echo $x
done
$ ./test.sh -a -b hello
-a -b hello
-a
-b
hello
$opt
不是内置变量或特殊变量。如果在脚本中提到它(在 for opt
之外没有 in something
部分,export
ed 在 运行 脚本之前。