在单独的行bash的打印数组元素 [英] bash print array elements on separate lines

查看:352
本文介绍了在单独的行bash的打印数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打印在不同的行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..." (where c is the first char of IFS).

You almost always want "$@". Same goes for "${arr[@]}".

Always quote them!

这篇关于在单独的行bash的打印数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆