$*:当用"$*"时,$*表示把所有参数作用一个字符串。
$@:当用"$@"时,$@表示把和每个参数作为一个单独的字符串。
#example:
#test.sh
#!/bin/bash
#---------------------------------------------
#This shell script test the difference between $* and $@
#---------------------------------------------
E_BADARGS=65
if [ ! -n "$1" ]
then
echo "Usage:`basename $0` argument1 argument2 etc."
exit $E_BADARGS
fi
echo
index=1
echo "Listing args with/"/$*/":"
for arg in "$*"
do
echo "Arg #$index = $arg"
echo "next line?"
let "index+=1"
done
echo "Entire arg list seen as a single word"
index=1
echo "Listing args with /$*:"
for arg in $*
do
echo "Arg #$index = $arg"
let "indx+=1"
done
echo "Entire arg list seen as separate words"
index=1
echo "Listing args with /"/$@/":"
for arg in "$@"
do
echo "Arg #$index = $arg"
let "index+=1"
done
echo "Entire arg list seen as separate words"
index=1
echo "Listing args with /$@:"
for arg in $@
do
echo "Arg #$index = $arg"
let "index+=1"
done
echo "Entire arg list seen as separate words"
exit 0
$chmod +x test.sh
$./test.sh hello nice to meet you
the output is :
Listing args with"$*":
Arg #1 = hello nice to meet you
next line?
Entire arg list seen as a single word
Listing args with $*:
Arg #1 = hello
Arg #1 = nice
Arg #1 = to
Arg #1 = meet
Arg #1 = you
Entire arg list seen as separate words
Listing args with "$@":
Arg #1 = hello
Arg #2 = nice
Arg #3 = to
Arg #4 = meet
Arg #5 = you
Entire arg list seen as separate words
Listing args with $@:
Arg #1 = hello
Arg #2 = nice
Arg #3 = to
Arg #4 = meet
Arg #5 = you
Entire arg list seen as separate words
但有些时候$*and $@的输出会不一致,这取决于IFS的设置
for example:
#test2.sh
#!/bin/bash
set -- "First one" "second" "third:one" "" "Fifth: :one"
echo
echo "IFS unchanged, using $/$*"
c=0
for i in "$*" # quoted
do echo "$((c+=1)): [$i]" # This line remains the same in every instance.
# Echo args.
done
echo ---
echo 'IFS unchanged, using $*'
c=0
for i in $* # unquoted
do echo "$((c+=1)): [$i]"
done
echo ---
echo 'IFS unchanged, using "$@"'
c=0
for i in "$@"
do echo "$((c+=1)): [$i]"
done
echo ---
echo 'IFS unchanged, using $@'
c=0
for i in $@
do echo "$((c+=1)): [$i]"
done
echo ---
IFS=:
echo 'IFS=":", using "$*"'
c=0
for i in "$*"
do echo "$((c+=1)): [$i]"
done
echo ---
echo 'IFS=":", using $*'
c=0
for i in $*
do echo "$((c+=1)): [$i]"
done
echo ---
var=$*
echo 'IFS=":", using "$var" (var=$*)'
c=0
for i in "$var"
do echo "$((c+=1)): [$i]"
done
echo ---
echo 'IFS=":", using $var (var=$*)'
c=0
for i in $var
do echo "$((c+=1)): [$i]"
done
echo ---
var="$*"
echo 'IFS=":", using $var (var="$*")'
c=0
for i in $var
do echo "$((c+=1)): [$i]"
done
echo ---
echo 'IFS=":", using "$var" (var="$*")'
c=0
for i in "$var"
do echo "$((c+=1)): [$i]"
done
echo ---
echo 'IFS=":", using "$@"'
c=0
for i in "$@"
do echo "$((c+=1)): [$i]"
done
echo ---
echo 'IFS=":", using $@'
c=0
for i in $@
do echo "$((c+=1)): [$i]"
done
echo ---
var=$@
echo 'IFS=":", using $var (var=$@)'
c=0
for i in $var
do echo "$((c+=1)): [$i]"
done
echo ---
echo 'IFS=":", using "$var" (var=$@)'
c=0
for i in "$var"
do echo "$((c+=1)): [$i]"
done
echo ---
var="$@"
echo 'IFS=":", using "$var" (var="$@")'
c=0
for i in "$var"
do echo "$((c+=1)): [$i]"
done
echo ---
echo 'IFS=":", using $var (var="$@")'
c=0
for i in $var
do echo "$((c+=1)): [$i]"
done
echo
exit 0
The output as follows:
IFS unchanged, using "$*"
1: [First one second third:one Fifth: :one]
---
IFS unchanged, using $*
1: [First]
2: [one]
3: [second]
4: [third:one]
5: [Fifth:]
6: [:one]
---
IFS unchanged, using "$@"
1: [First one]
2: [second]
3: [third:one]
4: []
5: [Fifth: :one]
---
IFS unchanged, using $@
1: [First]
2: [one]
3: [second]
4: [third:one]
5: [Fifth:]
6: [:one]
---
IFS=":", using "$*"
1: [First one:second:third:one::Fifth: :one]
---
IFS=":", using $*
1: [First one]
2: [second]
3: [third]
4: [one]
5: []
6: [Fifth]
7: [ ]
8: [one]
---
IFS=":", using "$var" (var=$*)
1: [First one:second:third:one::Fifth: :one]
---
IFS=":", using $var (var=$*)
1: [First one]
2: [second]
3: [third]
4: [one]
5: []
6: [Fifth]
7: [ ]
8: [one]
---
IFS=":", using $var (var="$*")
1: [First one]
2: [second]
3: [third]
4: [one]
5: []
6: [Fifth]
7: [ ]
8: [one]
---
IFS=":", using "$var" (var="$*")
1: [First one:second:third:one::Fifth: :one]
---
IFS=":", using "$@"
1: [First one]
2: [second]
3: [third:one]
4: []
5: [Fifth: :one]
---
IFS=":", using $@
1: [First one]
2: [second]
3: [third]
4: [one]
5: []
6: [Fifth]
7: [ ]
8: [one]
---
IFS=":", using $var (var=$@)
1: [First one second third one Fifth one]
---
IFS=":", using "$var" (var=$@)
1: [First one second third one Fifth one]
---
IFS=":", using "$var" (var="$@")
1: [First one second third:one Fifth: :one]
---
IFS=":", using $var (var="$@")
1: [First one second third]
2: [one Fifth]
3: [ ]
4: [one]
Pleasee taste it slowly!
摘自abs_guide_en
135

被折叠的 条评论
为什么被折叠?



