什么是bash中命令行参数的$ opt变量 [英] What is $opt variable for command line parameter in bash
问题描述
我有下面的bash脚本,我对$ opt的含义有些困惑.搜索后我没有找到有关它的详细信息.
I have below bash script, I am a bit confuse about what is $opt meaning. and I do not find detail information about it after search.
test.sh:
echo "opt:" $opt
for opt; do
echo $opt
done
echo "opt:" $opt
for opt; do
echo $opt
done
输出:
./test.sh -a -b c
opt:
-a
-b
c
output:
./test.sh -a -b c
opt:
-a
-b
c
推荐答案
来自 bash(1)手册页:
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循环将迭代脚本的位置参数,即$1
,$2
,$3
,....
So, if the in
word is missing the for loop iterates over the positional arguments to the script, i.e. $1
, $2
, $3
, ....
名称opt
没什么特别的,可以使用任何合法的变量名称.考虑一下此脚本及其输出:
There is nothing special about the name opt
, any legal variable name can be used. Consider this script and its output:
#test.sh
echo $* # output all positional parameters $1, $2,...
for x
do
echo $x
done
$ ./test.sh -a -b hello
-a -b hello
-a
-b
hello
这篇关于什么是bash中命令行参数的$ opt变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!