How do I print the array element of a Bash array on separate lines? This one works, but surely there is a better way:
如何在单独的行上打印Bash数组的数组元素?这一招行之有效,但肯定有更好的办法:
$ 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
5 个解决方案
#1
255
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.
未引用的结果未说明。在Bash中,两者都扩展为分开的args,然后是wordsplit和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
).引用“$@”将每个元素扩展为单独的参数,而“$*”扩展为合并为一个参数的args:“$1c$2c…”(其中c是IFS的第一个字符)。
You almost always want "$@"
. Same goes for "${arr[@]}"
.
你几乎总是想要“$@”。同样适用于“$ { arr[@]}”。
Always quote them!
总报价!
#2
51
Just quote the argument to echo:
引用这个论点来回应
( IFS=$'\n'; echo "${my_array[*]}" )
the sub shell helps restoring the IFS after use
子shell帮助在使用后恢复IFS。
#3
25
Using for:
使用:
for each in "${alpha[@]}"
do
echo "$each"
done
Using history; note this will fail if your values contain !
:
使用历史;注意,如果您的值包含以下内容,此操作将失败!
history -p "${alpha[@]}"
Using basename; note this will fail if your values contain /
:
使用:;注意,如果您的值包含/:
basename -a "${alpha[@]}"
Using shuf; note that results might not come out in order:
使用shuf;注意,结果可能不是按顺序出来的:
shuf -e "${alpha[@]}"
#4
3
I tried the answers here in a giant for...if loop, but didn't get any joy - so I did it like this, maybe messy but did the job:
我在这里用一个巨大的盒子尝试了答案……如果是循环,但没有得到任何乐趣——我这样做了,可能有点乱,但做了工作:
# EXP_LIST2 is iterated
# imagine a for loop
EXP_LIST="List item"
EXP_LIST2="$EXP_LIST2 \n $EXP_LIST"
done
echo -e $EXP_LIST2
although that added a space to the list, which is fine - I wanted it indented a bit. Also presume the "\n" could be printed in the original $EP_LIST.
虽然这给列表增加了一个空格,但这是可以的——我希望它缩进一点。也假定“\n”可以打印在原始的$EP_LIST中。
#5
2
Another useful variant is pipe to tr
:
另一个有用的变体是管道到tr:
echo ${my_array[@]} | tr " " "\n"
echo ${my_array[@]
This looks simple and compact
这看起来简单而紧凑
#1
255
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.
未引用的结果未说明。在Bash中,两者都扩展为分开的args,然后是wordsplit和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
).引用“$@”将每个元素扩展为单独的参数,而“$*”扩展为合并为一个参数的args:“$1c$2c…”(其中c是IFS的第一个字符)。
You almost always want "$@"
. Same goes for "${arr[@]}"
.
你几乎总是想要“$@”。同样适用于“$ { arr[@]}”。
Always quote them!
总报价!
#2
51
Just quote the argument to echo:
引用这个论点来回应
( IFS=$'\n'; echo "${my_array[*]}" )
the sub shell helps restoring the IFS after use
子shell帮助在使用后恢复IFS。
#3
25
Using for:
使用:
for each in "${alpha[@]}"
do
echo "$each"
done
Using history; note this will fail if your values contain !
:
使用历史;注意,如果您的值包含以下内容,此操作将失败!
history -p "${alpha[@]}"
Using basename; note this will fail if your values contain /
:
使用:;注意,如果您的值包含/:
basename -a "${alpha[@]}"
Using shuf; note that results might not come out in order:
使用shuf;注意,结果可能不是按顺序出来的:
shuf -e "${alpha[@]}"
#4
3
I tried the answers here in a giant for...if loop, but didn't get any joy - so I did it like this, maybe messy but did the job:
我在这里用一个巨大的盒子尝试了答案……如果是循环,但没有得到任何乐趣——我这样做了,可能有点乱,但做了工作:
# EXP_LIST2 is iterated
# imagine a for loop
EXP_LIST="List item"
EXP_LIST2="$EXP_LIST2 \n $EXP_LIST"
done
echo -e $EXP_LIST2
although that added a space to the list, which is fine - I wanted it indented a bit. Also presume the "\n" could be printed in the original $EP_LIST.
虽然这给列表增加了一个空格,但这是可以的——我希望它缩进一点。也假定“\n”可以打印在原始的$EP_LIST中。
#5
2
Another useful variant is pipe to tr
:
另一个有用的变体是管道到tr:
echo ${my_array[@]} | tr " " "\n"
echo ${my_array[@]
This looks simple and compact
这看起来简单而紧凑