Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine arrays in the beginning of a for-loop (Bash)

Is it possible to do something like this?

a=( 1 2 3 )
b=( 4 5 6 )
for num in ( ${a[@]} ${b[@]} ) # or: for num in ${( ${a[@]} ${b[@]} )[@]}
do
    echo "$num"
done
# Outputs 1 2 3 4 5 6

I know you can combine them before and then loop through them, but is it possible in only one line?

Current solution:

a=( 1 2 3 )
b=( 4 5 6 )
c=( ${a[@]} ${b[@]} )
for num in ${c[@]}
do
    echo "$num"
done
# Outputs 1 2 3 4 5 6
like image 649
Tyilo Avatar asked May 30 '11 20:05

Tyilo


1 Answers

Specify both arrays.

for num in "${a[@]}" "${b[@]}"
 ...
like image 114
Ignacio Vazquez-Abrams Avatar answered Nov 15 '22 07:11

Ignacio Vazquez-Abrams