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
どうすればいいですか?
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
どうすればいいですか?
$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 "$*"
つまり、スペースで区切られたコマンドライン引数全体を出力します(または、技術的には、の値が何であれ$IFS
)。それを変数に格納したい場合は、次のことができます
thevar="$*"
それがあなたの質問に十分に答えられない場合、他に何を言うべきかわかりません...