betseg
betseg

Reputation: 165

Bash - arrays in a for loop

I'm trying to make a shell script but I'm stuck.

This doesn't work and I don't know why.

a=( "1" "2" )
b=( "3" "4" )
c=( "$a" "$b" )

for d in "${c[@]}"; do
    echo "${d[1]}"
done

Upvotes: 1

Views: 88

Answers (1)

Cyrus
Cyrus

Reputation: 88583

With bash:

a=( "1" "2" )
b=( "3" "4" )

# concatenate array a and b to new array c
c=( "${a[@]}" "${b[@]}" )

for d in "${c[@]}"; do 
  echo "$d"
done

Output:

1
2
3
4

Upvotes: 2

Related Questions