Linux 我想将Bash脚本的所有命令行参数存储到单个变量中
假设我有一个名为foo.sh的Bash脚本 我想这样称呼它:Linux 我想将Bash脚本的所有命令行参数存储到单个变量中,linux,bash,shell,unix,command-line-interface,Linux,Bash,Shell,Unix,Command Line Interface,假设我有一个名为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的值是多少)。如果要将其存储到变量中,可以这样做 th
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="$*"
如果这还不能很好地回答您的问题,我不知道还能说些什么……看看
$*
变量。它将所有命令行参数合并为一个参数
echo "$*"
这应该是你想要的
如果要避免涉及$IFS,请使用$@(或不要将$*括在引号中) 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
请注意,如果$IFS的赋值是在$splatvar赋值之后进行的,那么所有输出都是相同的($IFS在“atsplat2”示例中没有效果)
$ 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