本脚本是《Advanced Bash-Scripting Guide》第9章的一个示例,它的作用主要是说明$*和$@在使用上的差别和注意事项,并作出总结。
- #!/bin/bash
- # $*和$@仅在被双引号括起来时才有区别:
- # 1、如果$*被双引号括起来,则参数列表整个变成单个字符串。
- # 2、如果$@被双引号括起来,则参数列表中的每个参数均是一个单独的字符串。
- # 3、无引号时,两者功效相同,均以空格进行分隔。
- # IFC 即分隔符,默认为空格。
- # 1、指定IFC,有引号内的$*或者变量与未指定IFC一样,整个参数列表依然是单个字符串。
- # 2、指定IFC,无引号$*则在原参数基础上,对含有分隔符的参数进行分隔。
- set -- "First one" "second" "third:one" "" "Fifth: :one"
- # 脚本文件内设置脚本参数的一种方式, 作用等同于 shellfile aa bb cc。
- # 以上面方式设置参数后,则脚本名后带的参数无效,即aa bb cc无效。
- echo
- echo 'IFS unchanged, using "$*"'
- c=0
- for i in "$*" # 双引号
- do echo "$((c+=1)): [$i]"
- done
- echo --------------------
- # 输出结果如下:
- # 1: [First one second third:one Fifth: :one]
- echo 'IFS unchanged, using $*'
- c=0
- for i in $* # 无双引号,默认以空格分隔
- do echo "$((c+=1)): [$i]"
- done
- echo --------------------
- # "First one" "second" "third:one" "" "Fifth: :one"
- # 输出结果如下:
- # 1: [First]
- # 2: [one]
- # 3: [second]
- # 4: [third:one]
- # 5: [Fifth:]
- # 6: [:one]
- echo 'IFS unchanged, using "$@"'
- c=0
- for i in "$@" # 有双引号,原引号内的内容作为一个元素
- do echo "$((c+=1)): [$i]"
- done
- echo --------------------
- # "First one" "second" "third:one" "" "Fifth: :one"
- # 输出结果如下:
- # 1: [First one]
- # 2: [second]
- # 3: [third:one]
- # 4: []
- # 5: [Fifth: :one]
- echo 'IFS unchanged, using $@'
- c=0
- for i in $@ # 输出内容与$*相同
- do echo "$((c+=1)): [$i]"
- done
- echo --------------------
- # "First one" "second" "third:one" "" "Fifth: :one"
- # 输出结果如下:
- # 1: [First]
- # 2: [one]
- # 3: [second]
- # 4: [third:one]
- # 5: [Fifth:]
- # 6: [:one]
- # 指定分隔符对双引号内的$*或者其变量无效
- IFS=:
- echo 'IFS=":", using "$*"'
- c=0
- for i in "$*"
- do echo "$((c+=1)): [$i]"
- done
- echo --------------------
- # "First one" "second" "third:one" "" "Fifth: :one"
- # 输出结果如下:
- # 1: [First one:second:third:one::Fifth: :one]
- # 指定IFC,无引号$*则在原参数基础上,对含有分隔符的参数进行分隔
- echo 'IFS=":", using $*'
- c=0
- for i in $*
- do echo "$((c+=1)): [$i]"
- done
- echo --------------------
- # "First one" "second" "third:one" "" "Fifth: :one"
- # 输出结果如下:
- # 1: [First one]
- # 2: [second]
- # 3: [third]
- # 4: [one]
- # 5: []
- # 6: [Fifth]
- # 7: [ ]
- # 8: [one]
- # 指定分隔符对双引号内的$*或者其变量无效
- var=$*
- echo 'IFS=":", using "$var" (var=$*)'
- c=0
- for i in "$var" # 跟直接“$*”输出相同
- do echo "$((c+=1)): [$i]"
- done
- echo --------------------
- # "First one" "second" "third:one" "" "Fifth: :one"
- # 输出结果如下:
- # 1: [First one:second:third:one::Fifth: :one]
- echo 'IFS=":", using $var (var=$*)'
- c=0
- for i in $var # 跟直接$*输出相同
- do echo "$((c+=1)): [$i]"
- done
- echo --------------------
- # "First one" "second" "third:one" "" "Fifth: :one"
- # 输出结果如下:
- # 1: [First one]
- # 2: [second]
- # 3: [third]
- # 4: [one]
- # 5: []
- # 6: [Fifth]
- # 7: [ ]
- # 8: [one]
- var="$*"
- echo 'IFS=":", using $var (var="$*")'
- c=0
- for i in $var
- do echo "$((c+=1)): [$i]"
- done
- echo --------------------
- # "First one" "second" "third:one" "" "Fifth: :one"
- # 输出结果如下:
- # 1: [First one]
- # 2: [second]
- # 3: [third]
- # 4: [one]
- # 5: []
- # 6: [Fifth]
- # 7: [ ]
- # 8: [one]
- echo 'IFS=":", using "$var" (var="$*")'
- c=0
- for i in "$var"
- do echo "$((c+=1)): [$i]"
- done
- echo --------------------
- # "First one" "second" "third:one" "" "Fifth: :one"
- # 输出结果如下:
- # 1: [First one:second:third:one::Fifth: :one]
- echo 'IFS=":", using "$@"'
- c=0
- for i in "$@"
- do echo "$((c+=1)): [$i]"
- done
- echo --------------------
- # "First one" "second" "third:one" "" "Fifth: :one"
- # 输出结果如下:
- # 1: [First one]
- # 2: [second]
- # 3: [third:one]
- # 4: []
- # 5: [Fifth: :one]
- echo 'IFS=":", using $@'
- c=0
- for i in $@
- do echo "$((c+=1)): [$i]"
- done
- echo --------------------
- # "First one" "second" "third:one" "" "Fifth: :one"
- # 输出结果如下:
- # 1: [First one]
- # 2: [second]
- # 3: [third]
- # 4: [one]
- # 5: []
- # 6: [Fifth]
- # 7: [ ]
- # 8: [one]
- var=$@
- echo 'IFS=":", using $var (var=$@)'
- c=0
- for i in $var
- do echo "$((c+=1)): [$i]"
- done
- echo --------------------
- # "First one" "second" "third:one" "" "Fifth: :one"
- # 输出结果如下:
- # 1: [First one second third one Fifth one]
- echo 'IFS=":", using "$var" (var=$@)'
- c=0
- for i in "$var"
- do echo "$((c+=1)): [$i]"
- done
- echo --------------------
- # "First one" "second" "third:one" "" "Fifth: :one"
- # 输出结果如下:
- # 1: [First one second third one Fifth one]
- var="$@"
- echo 'IFS=":", using "$var" (var="$@")'
- c=0
- for i in "$var"
- do echo "$((c+=1)): [$i]"
- done
- echo --------------------
- # "First one" "second" "third:one" "" "Fifth: :one"
- # 输出结果如下:
- # 1: [First one second third:one Fifth: :one]
- echo 'IFS=":", using $var (var="$@")'
- c=0
- for i in $var
- do echo "$((c+=1)): [$i]"
- done
- # "First one" "second" "third:one" "" "Fifth: :one"
- # 输出结果如下:
- # 1: [First one second third]
- # 2: [one Fifth]
- # 3: [ ]
- # 4: [one]
- echo
- exit 0
- # This example script by Stephane Chazelas,
- # and slightly modified by the document author.
转载于:https://blog.51cto.com/waterman/851605