0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【ShellScript】$*と$@の違い

Posted at

$*$@の違い

クォートしない場合には$*$@は同じ挙動になります。

test.sh
outputarg() {
  echo "引数の数は $# 個です。"
  echo "第一引数は $1 です。"
  echo "第二引数は ${2:-なし} です。"
}

echo 'クォートなし'
echo '$@'
outputarg $@
echo '--------------------------'
echo '$*'
outputarg $*

$ bash test.sh 'arg1 arg2' 'arg3 arg4'
$@
引数の数は 4 個です。
第一引数は arg1 です。
第二引数は arg2 です。
--------------------------
$*
引数の数は 4 個です。
第一引数は arg1 です。
第二引数は arg2 です。

"$*""$@"の違い

クォートした場合には"$*"は引数が結合され、"$@"は引数が渡したとおりに分割されます。

test.sh
outputarg() {
  echo "引数の数は $# 個です。"
  echo "第一引数は $1 です。"
  echo "第二引数は ${2:-なし} です。"
}

echo '"$@"'
outputarg "$@"
echo '--------------------------'
echo '"$*"'
outputarg "$*"

$ bash test.sh 'arg1 arg2' 'arg3 arg4'
"$@"
引数の数は 2 個です。
第一引数は arg1 arg2 です。
第二引数は arg3 arg4 です。
--------------------------
"$*"
引数の数は 1 個です。
第一引数は arg1 arg2 arg3 arg4 です。
第二引数は なし です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?