![コマンドのパラメータをスクリプトに転送するために2つの変数($ @、$ *)を使用する目的は何ですか? [コピー]](https://linux33.com/image/61211/%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89%E3%81%AE%E3%83%91%E3%83%A9%E3%83%A1%E3%83%BC%E3%82%BF%E3%82%92%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%81%AB%E8%BB%A2%E9%80%81%E3%81%99%E3%82%8B%E3%81%9F%E3%82%81%E3%81%AB2%E3%81%A4%E3%81%AE%E5%A4%89%E6%95%B0%EF%BC%88%24%20%40%E3%80%81%24%20*%EF%BC%89%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%99%E3%82%8B%E7%9B%AE%E7%9A%84%E3%81%AF%E4%BD%95%E3%81%A7%E3%81%99%E3%81%8B%EF%BC%9F%20%5B%E3%82%B3%E3%83%94%E3%83%BC%5D.png)
で使用されているスクリプト$@
と特殊変数を読みました$*
。私が理解したところ、スクリプトを実行するときに使用されるパラメータは、$@
すべてのパラメータが存在する場合は$*
スクリプト内でアクセスできるように2つの特殊変数に格納されます。
同じパラメータセットに2つの特殊変数が必要な理由を理解できません。 1つの特殊変数を使用する場合と別の特殊変数を使用する場合の違いは何ですか?
答え1
簡単で独創的な説明は次のとおりです。
$*
設定されたすべてのパラメータは文字列です(パラメータは最初の文字で区切られています$IFS
)。$@
各パラメータは異なる文字列です(改行文字で区切られたパラメータ)。
からman bash
:
* Expands to the positional parameters, starting from one. When the expansion is not within dou‐
ble quotes, each positional parameter expands to a separate word. In contexts where it is per‐
formed, those words are subject to further word splitting and pathname expansion. When the
expansion occurs within double quotes, it expands to a single word with the value of each param‐
eter separated by the first character of the IFS special variable. That is, "$*" is equivalent
to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is
unset, the parameters are separated by spaces. If IFS is null, the parameters are joined with‐
out intervening separators.
@ Expands to the positional parameters, starting from one. When the expansion occurs within dou‐
ble quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2"
... If the double-quoted expansion occurs within a word, the expansion of the first parameter
is joined with the beginning part of the original word, and the expansion of the last parameter
is joined with the last part of the original word. When there are no positional parameters,
"$@" and $@ expand to nothing (i.e., they are removed).
答え2
さらに:
"$*"
次に展開"arg1 arg2 arg3 …"
"$@"
次に展開"arg1" "arg2" "arg3" …
したがって、より"$@"
安全です。$*
古いことがあり、以前のバージョンとの互換性のために存在します。
答え3
からman bash
:
Special Parameters
The shell treats several parameters specially. These parameters may only
be referenced; assignment to them is not allowed.
* Expands to the positional parameters, starting from one. When the
expansion is not within double quotes, each positional parameter
expands to a separate word. In contexts where it is performed,
those words are subject to further word splitting and pathname
expansion. When the expansion occurs within double quotes, it
expands to a single word with the value of each parameter separated
by the first character of the IFS special variable. That is, "$*"
is equivalent to "$1c$2c...", where c is the first character of the
value of the IFS variable. If IFS is unset, the parameters are
separated by spaces. If IFS is null, the parameters are joined
without intervening separators.
@ Expands to the positional parameters, starting from one. When the
expansion occurs within double quotes, each parameter expands to a
separate word. That is, "$@" is equivalent to "$1" "$2" ... If
the double-quoted expansion occurs within a word, the expansion of
the first parameter is joined with the beginning part of the origi‐
nal word, and the expansion of the last parameter is joined with
the last part of the original word. When there are no positional
parameters, "$@" and $@ expand to nothing (i.e., they are removed).
4つの場合、特にスペースを含むパラメータを比較してください。
for i in $*; do echo "$i"; done
for i in $@; do echo "$i"; done
for i in "$*"; do echo "$i"; done
for i in "$@"; do echo "$i"; done
答え4
特殊パラメータ $* と $@:
すべてのコマンドラインパラメータに一度にアクセスできる特別なパラメータがあります。 $ *と$ @は二重引用符 ""で囲まない限り同じ効果を持ちます。
両方のパラメータはすべてのコマンドラインパラメータを指定しますが、特殊文字「$ *」はリスト全体を空白の1つのパラメータとして使用し、「$ @」特殊パラメータはリスト全体を別々のパラメータとして使用します。に分割します。
$ *または$ @特殊パラメータを使用してシェルスクリプトを作成して、不明な数のコマンドライン引数を処理できます。