[英]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.