让我们说我有一个名为bash脚本的 foo.sh

我想这样称呼它

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

请注意的是,如果分配到$ IFS进行的$ splatvar分配后进行,则所有的输出将是相同的($ IFS将具有在“atsplat2”例如没有影响)。

有一个在所述$*变量。它结合了所有的命令行参数为一体。

echo "$*"

这应该做你想要什么。

更多资讯。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top