在单独的行bash的打印数组元素 [英] bash print array elements on separate lines
问题描述
如何打印在不同的行Bash的数组的数组元素?这一件作品,但肯定有一个更好的方法:
$ my_array =(一二三)
$因为我在$ {my_array [@]};做回声$ I; DONE
一
二
三
试过这一个,但它没有工作:
$ IFS = $'\\ N'回声$ {my_array [*]}
一二三
试着做这样的:
$的printf'%s的\\ n'$ {my_array [@]}
之间的差异 $ @
和 $ *
:
-
非上市,结果是不确定的。在bash中,既扩大分离ARGS
然后wordsplit和晶片被保护。 -
引用
$ @
$ *
扩展到ARGS合并成一个参数:$ 1C $ 2C ......
(其中C
是
的第一个字符IFS
)。
您几乎总是需要$ @
。同样适用于$ {ARR [@]}
。
总是给他们!
How do I print the array element of a Bash array on separate lines? This one works, but surely there is a better way:
$ my_array=(one two three)
$ for i in ${my_array[@]}; do echo $i; done
one
two
three
Tried this one but it did not work:
$ IFS=$'\n' echo ${my_array[*]}
one two three
Try doing this :
$ printf '%s\n' "${my_array[@]}"
The difference between $@
and $*
:
Unquoted, the results are unspecified. In Bash, both expand to separate args and then wordsplit and globbed.
Quoted,
"$@"
expands each element as a separate argument, while"$*"
expands to the args merged into one argument:"$1c$2c..."
(wherec
is the first char ofIFS
).
You almost always want "$@"
. Same goes for "${arr[@]}"
.
Always quote them!
这篇关于在单独的行bash的打印数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!