gpt4 book ai didi

linux - bash array using @ vs *,两者的区别

转载 作者:太空狗 更新时间:2023-10-29 11:31:03 25 4
gpt4 key购买 nike

我无法确定使用 ${array[@]} 与 ${array[*]} 之间的确切区别

我看到的是打印时的差异,但我想还有更多差异

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

我在 TLDP 上搜索了它,但无法弄清楚。它是一般的 bash 东西还是只用于数组?非常感谢!

最佳答案

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.

例子:

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

关于linux - bash array using @ vs *,两者的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52590446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com