Dimitrie Mititelu
Dimitrie Mititelu

Reputation: 1071

bash array using @ vs *, difference between the two

I cannot pinpoint the exact difference between using ${array[@]} vs ${array[*]}

What diff I see is when printing, but I guess there is more to it

declare -a array
array=("1" "2" "3")
IFS=","
printf "%s" ${array[@]}
printf "%s" ${array[*]}
IFS=" "

I searched on TLDP about it, but couldn't figure it out. Is it a general bash thing or just for arrays? Thanks a lot!

Upvotes: 22

Views: 6771

Answers (1)

oliv
oliv

Reputation: 13239

As mentioned in man bash:

If the word is double-quoted, ${name[*]} expands to a single word with the value of each array member separated by the first character of the IFS special variable, and ${name[@]} expands each element of name to a separate word.

Examples:

array=("1" "2" "3")
printf "'%s'" "${array[*]}"
'1 2 3'
printf "'%s'" "${array[@]}"
'1''2''3'

Upvotes: 27

Related Questions