Bash array variables: [@] or [*]? -
bash-3.2$ echo astr | sed 'hah' | sed 's/s/z/' sed: 1: "hah": characters @ end of h command bash-3.2$ echo ${pipestatus[*]} 0 1 0 bash-3.2$ echo astr | sed 'hah' | sed 's/s/z/' sed: 1: "hah": characters @ end of h command bash-3.2$ piperet=("${pipestatus[*]}") bash-3.2$ echo ${piperet[*]} 0 1 0 bash-3.2$
this indicates [*]
works fine. this tut mentions use [@]
instead.
are both equally valid?
the difference matters when array elements contain spaces etc. , multiple spaces, , manifest when expressions enclosed in double quotes:
$ x=( ' b c ' 'd e f' ) $ printf "[%s]\n" "${x[*]}" [ b c d e f] $ printf "[%s]\n" "${x[@]}" [ b c ] [d e f] $ printf "[%s]\n" ${x[@]} [a] [b] [c] [d] [e] [f] $ printf "[%s]\n" ${x[*]} [a] [b] [c] [d] [e] [f] $
outside double quotes, there's no difference. inside double quotes, *
means 'a single string' , @
means 'array elements individually'.
it closely analogous way $*
, $@
(and "$*"
, "$@"
) work.
see bash
manual on:
Comments
Post a Comment