阅读了高级bash脚本编程一书,贴上有关$#, $*, $@的相关内容已经脚本实例。
位置参数:$1, $2.. : 位置参数从参数 0 开始。参数 0 引用启动 bash 的程序的名称,如果函数在 shell 脚本中运行,则引用
shell 脚本的名称。有关该参数的其他信息,比如 bash 由 -c 参数启动,请参阅 bash 手册页面。由单引号或
双引号包围的字符串被作为一个参数进行传递,传递时会去掉引号。
如果是双引号,则在调用函数之前将对
$HOME 之类的 shell 变量进行扩展。对于包含嵌入空白或其他字符(这些空白或字符可能对 shell 有特殊意
义)的参数,需要使用单引号或双引号进行传递。
$* : 位置参数从参数 1 开始。如果在双引号中进行扩展,则扩展就是一个词,由 IFS 特殊变量的第一个字符
将参数分开,如果 IFS
为空,则没有间隔空格。IFS 的默认值是空白、制表符和换行符。如果没有设置 IFS,
则使用空白作为分隔符(仅对默认 IFS 而言)。
$@ : 位置参数从参数 1 开始。如果在双引号中进行扩展,则每个参数都会成为一个词,因此 “$@” 与
“$1” “$2” 等效。如果参数有可能包含嵌入空白,那么您将需要使用这种形式。
$# : 参数数量(不包含参数 0)。
脚本实例: 程序实例是最能说明问题的了
- #!/bin/bash
- # "$*"和"$@"的古怪行为,
- #+ 依赖于它们是否被""引用.
- # 单词拆分和换行的不一致处理.
- set -- "First one" "second" "third:one" "" "Fifth: :one"
- # 设置这个脚本参数,$1,$2,等等.
- echo $#
- #################################
- echo
- echo "eg: 1-1"
- echo 'IFS unchanged, using "$*"'
- c=0
- for i in "$*" # 引用
- do echo "$((c+=1)): [$i]" # 这行在下边的每个例子中都一样.
- # Echo 参数.
- done
- echo ---
- #-------------------------------
- echo "eg: 1-2"
- echo 'IFS unchanged, using $*'
- c=0
- for i in $* # 未引用
- do echo "$((c+=1)): [$i]"
- done
- echo ---
- #################################
- #################################
- echo
- echo "eg: 2-1"
- echo 'IFS unchanged, using "$@"'
- c=0
- for i in "$@"
- do echo "$((c+=1)): [$i]"
- done
- echo ---
- #-------------------------------
- echo "eg: 2-2"
- echo 'IFS unchanged, using $@'
- c=0
- for i in $@
- do echo "$((c+=1)): [$i]"
- done
- echo ---
- #################################
- #################################
- echo
- echo "eg: 3-1"
- IFS=:
- echo 'IFS=":", using "$*"'
- c=0
- for i in "$*"
- do echo "$((c+=1)): [$i]"
- done
- echo ---
- #-------------------------------
- echo "eg: 3-2"
- echo 'IFS=":", using $*'
- c=0
- for i in $*
- do echo "$((c+=1)): [$i]"
- done
- echo ---
- #################################
- #################################
- echo
- echo "eg: 4-1"
- var=$*
- echo 'IFS=":", using "$var" (var=$*)'
- c=0
- for i in "$var"
- do echo "$((c+=1)): [$i]"
- done
- echo ---
- #-------------------------------
- echo "eg: 4-2"
- echo 'IFS=":", using $var (var=$*)'
- c=0
- for i in $var
- do echo "$((c+=1)): [$i]"
- done
- echo ---
- #################################
- #################################
- echo
- echo "eg: 5-1"
- var="$*"
- echo 'IFS=":", using "$var" (var="$*")'
- c=0
- for i in "$var"
- do echo "$((c+=1)): [$i]"
- done
- echo ---
- #-------------------------------
- echo "eg: 5-2"
- echo 'IFS=":", using $var (var="$*")'
- c=0
- for i in $var
- do echo "$((c+=1)): [$i]"
- done
- echo ---
- #################################
- #################################
- echo
- echo "eg: 6-1"
- echo 'IFS=":", using "$@"'
- c=0
- for i in "$@"
- do echo "$((c+=1)): [$i]"
- done
- echo ---
- #-------------------------------
- echo "eg: 6-2"
- echo 'IFS=":", using $@'
- c=0
- for i in $@
- do echo "$((c+=1)): [$i]"
- done
- echo ---
- #################################
- #################################
- echo
- echo "eg: 7-1"
- var=$@
- echo 'IFS=":", using $var (var=$@)'
- c=0
- for i in $var
- do echo "$((c+=1)): [$i]"
- done
- echo ---
- #-------------------------------
- echo "eg: 7-2"
- echo 'IFS=":", using "$var" (var=$@)'
- c=0
- for i in "$var"
- do echo "$((c+=1)): [$i]"
- done
- echo ---
- #################################
- #################################
- echo
- echo "eg: 8-1"
- var=$@
- var="$@"
- echo 'IFS=":", using "$var" (var="$@")'
- c=0
- for i in "$var"
- do echo "$((c+=1)): [$i]"
- done
- echo ---
- #-------------------------------
- echo "eg: 8-2"
- echo 'IFS=":", using $var (var="$@")'
- c=0
- for i in $var
- do echo "$((c+=1)): [$i]"
- done
- #################################
- echo
- # 用 ksh 或者 zsh -y 来试试这个脚本.
- exit 0
- # This example script by Stephane Chazelas,
- # and slightly modified by the document author.
- ################################End
运行结果:- 5
- eg: 1-1
- IFS unchanged, using "$*"
- 1: [First one second third:one Fifth: :one]
- ---
- eg: 1-2
- IFS unchanged, using $*
- 1: [First]
- 2: [one]
- 3: [second]
- 4: [third:one]
- 5: [Fifth:]
- 6: [:one]
- ---
- eg: 2-1
- IFS unchanged, using "$@"
- 1: [First one]
- 2: [second]
- 3: [third:one]
- 4: []
- 5: [Fifth: :one]
- ---
- eg: 2-2
- IFS unchanged, using $@
- 1: [First]
- 2: [one]
- 3: [second]
- 4: [third:one]
- 5: [Fifth:]
- 6: [:one]
- ---
- eg: 3-1
- IFS=":", using "$*"
- 1: [First one:second:third:one::Fifth: :one]
- ---
- eg: 3-2
- IFS=":", using $*
- 1: [First one]
- 2: [second]
- 3: [third]
- 4: [one]
- 5: []
- 6: [Fifth]
- 7: [ ]
- 8: [one]
- ---
- eg: 4-1
- IFS=":", using "$var" (var=$*)
- 1: [First one:second:third:one::Fifth: :one]
- ---
- eg: 4-2
- IFS=":", using $var (var=$*)
- 1: [First one]
- 2: [second]
- 3: [third]
- 4: [one]
- 5: []
- 6: [Fifth]
- 7: [ ]
- 8: [one]
- ---
- eg: 5-1
- IFS=":", using "$var" (var="$*")
- 1: [First one:second:third:one::Fifth: :one]
- ---
- eg: 5-2
- IFS=":", using $var (var="$*")
- 1: [First one]
- 2: [second]
- 3: [third]
- 4: [one]
- 5: []
- 6: [Fifth]
- 7: [ ]
- 8: [one]
- ---
- eg: 6-1
- IFS=":", using "$@"
- 1: [First one]
- 2: [second]
- 3: [third:one]
- 4: []
- 5: [Fifth: :one]
- ---
- eg: 6-2
- IFS=":", using $@
- 1: [First one]
- 2: [second]
- 3: [third]
- 4: [one]
- 5: []
- 6: [Fifth]
- 7: [ ]
- 8: [one]
- ---
- eg: 7-1
- IFS=":", using $var (var=$@)
- 1: [First one second third one Fifth one]
- ---
- eg: 7-2
- IFS=":", using "$var" (var=$@)
- 1: [First one second third one Fifth one]
- ---
- eg: 8-1
- IFS=":", using "$var" (var="$@")
- 1: [First one second third:one Fifth: :one]
- ---
- eg: 8-2
- IFS=":", using $var (var="$@")
- 1: [First one second third]
- 2: [one Fifth]
- 3: [ ]
- 4: [one]
对于之前的1-6的实例,没有问题
但是对于实例7,为何作为$@直接传递给var后,var的值中却没有IFS的分割符显示,比如实例5-1?
阅读(389) | 评论(0) | 转发(0) |