Hutch
Hutch

Reputation: 163

printing array inside echo command

how to print myArray inside echo command?

declare -a myArray=([0]="AAA" [1]="AAC" [2]="AAG" [3]="AAU" [4]="ACA" [5]="ACC" [6]="ACG" [7]="ACU" [8]="AGA" [9]="AGC" [10]="AGG" [11]="AGU" [12]="AUA" [13]="AUC")

I know it can be done in this way

echo ${myArray[@]}

or

for i in ${myArray[@]}
do
 echo $i
done

how to do it in a single echo command like this, (like in python)

echo ${  for i in ${myArray[@]} }  # does not work

Upvotes: 0

Views: 64

Answers (3)

Al-waleed Shihadeh
Al-waleed Shihadeh

Reputation: 2845

You can do it like this :

for i in ${myArray[@]}; do  echo $i; done

Upvotes: 0

KamilCuk
KamilCuk

Reputation: 140930

Use command subsitution

echo "$(for i in ${myArray[@]}; do echo $i; done)"

But I think you really mean to:

printf "%s\n" "${myArray[@]}"

Upvotes: 3

kojiro
kojiro

Reputation: 77059

Consider printf:

printf '%s\n' "${myArray[@]}"

But don't forget the quotes, or your expansion will be wordsplit when you don't want it to!

Upvotes: 4

Related Questions