[英]I would like to store all command-line arguments to a Bash script into a single variable
假设我有一个名为foo.sh的Bash脚本。
我想这样称呼它:
foo.sh Here is a bunch of stuff on the command-line
我希望将所有这些文本存储到单个变量中并打印出来。
所以我的输出是:
Here is a bunch of stuff on the command-line
我该怎么做?
echo "$*"
将执行您想要的操作,即打印出整个命令行参数,并以空格分隔(或者,从技术上讲,无论$IFS
的值如何)。 如果要将其存储到变量中,则可以执行
thevar="$*"
如果那还不能很好地回答您的问题,我不确定还有什么要说的。。。
如果要避免涉及$ IFS,请使用$ @(或不要将$ *括在引号中)
$ cat atsplat
IFS="_"
echo " at: $@"
echo " splat: $*"
echo "noquote: "$*
$ ./atsplat this is a test
at: this is a test
splat: this_is_a_test
noquote: this is a test
IFS行为也遵循变量分配。
$ cat atsplat2
IFS="_"
atvar=$@
splatvar=$*
echo " at: $atvar"
echo " splat: $splatvar"
echo "noquote: "$splatvar
$ ./atsplat2 this is a test
at: this is a test
splat: this_is_a_test
noquote: this is a test
请注意,如果在分配$ splatvar之后对$ IFS进行分配,则所有输出将是相同的($ IFS在“ atsplat2”示例中无效)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.