14

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

どうすればいいですか?

4

3 に答える 3

29

$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」の例では効果がありません)。

于 2009-05-09T00:00:13.530 に答える
27
echo "$*"

つまり、スペースで区切られたコマンドライン引数全体を出力します(または、技術的には、の値が何であれ$IFS)。それを変数に格納したい場合は、次のことができます

thevar="$*"

それがあなたの質問に十分に答えられない場合、他に何を言うべきかわかりません...

于 2009-05-08T20:37:35.077 に答える
0

$*変数を見てください。すべてのコマンド ライン引数を 1 つに結合します。

echo "$*"

これはあなたが望むことをするはずです。

詳細はこちら。

于 2009-05-08T20:38:48.163 に答える