喜欢将bash脚本中的所有命令行参数存储到单个变量中

时间:2022-10-09 15:41:48

Let's say I have a bash script called foo.sh.

假设我有一个名为foo.sh的bash脚本。

I'd like to call it like this

我想这样称呼它

foo.sh Here is a bunch of stuff on the command-line

and I'd like it to store all of that text into a single variable and print it out.

我希望它将所有文本存储到一个变量中并将其打印出来。

So my output would be:

所以我的输出将是:

Here is a bunch of stuff on the command-line

How would I do this?

我该怎么办?

3 个解决方案

#1


27  

echo "$*"

would do what you want, namely printing out the entire command-line arguments, separated by a space (or, technically, whatever the value of $IFS is). If you wanted to store it into a variable, you could do

会做你想要的,即打印出整个命令行参数,用空格分隔(或者,技术上,无论$ IFS的值是多少)。如果你想将它存储到变量中,你可以这样做

thevar="$*"

If that doesn't answer your question well enough, I'm not sure what else to say...

如果这不能很好地回答你的问题,我不知道还有什么可说的......

#2


26  

If you want to avoid having $IFS involved, use $@ (or don't enclose $* in quotes)

如果你想避免涉及$ 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

The IFS behavior follows variable assignments, too.

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

Note that if the assignment to $IFS were made after the assignment of $splatvar, then all the outputs would be the same ($IFS would have no effect in the "atsplat2" example).

注意,如果在分配$ splatvar之后分配给$ IFS,那么所有输出都是相同的($ IFS在“atsplat2”示例中没有效果)。

#3


0  

Have a look at the $* variable. It combines all command line arguments into one.

看看$ *变量。它将所有命令行参数合并为一个。

echo "$*"

This should do what you want.

这应该做你想要的。

More info here.

更多信息在这里。

#1


27  

echo "$*"

would do what you want, namely printing out the entire command-line arguments, separated by a space (or, technically, whatever the value of $IFS is). If you wanted to store it into a variable, you could do

会做你想要的,即打印出整个命令行参数,用空格分隔(或者,技术上,无论$ IFS的值是多少)。如果你想将它存储到变量中,你可以这样做

thevar="$*"

If that doesn't answer your question well enough, I'm not sure what else to say...

如果这不能很好地回答你的问题,我不知道还有什么可说的......

#2


26  

If you want to avoid having $IFS involved, use $@ (or don't enclose $* in quotes)

如果你想避免涉及$ 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

The IFS behavior follows variable assignments, too.

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

Note that if the assignment to $IFS were made after the assignment of $splatvar, then all the outputs would be the same ($IFS would have no effect in the "atsplat2" example).

注意,如果在分配$ splatvar之后分配给$ IFS,那么所有输出都是相同的($ IFS在“atsplat2”示例中没有效果)。

#3


0  

Have a look at the $* variable. It combines all command line arguments into one.

看看$ *变量。它将所有命令行参数合并为一个。

echo "$*"

This should do what you want.

这应该做你想要的。

More info here.

更多信息在这里。