Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use arrays in unix shell?

Tags:

shell

loops

unix

I use code in !/bin/sh like this:

LIST1="mazda toyota honda"
LIST2="2009 2006 2010"

for m in $LIST1
do
 echo $m
done

As you can see this currently prints out only the make of the car. How can I include the year to each "echo" based on the same positions so that I get results like:

mazda 2009

toyota 2006

honda 2010

?

like image 410
goe Avatar asked Jan 29 '26 03:01

goe


1 Answers

Assuming bash

array1=(mazda toyota honda)
array2=(2009 2006 2010)

for index in ${!array1[*]}
do
    printf "%s %s\n" ${array1[$index]} ${array2[$index]}
done
like image 60
Nifle Avatar answered Jan 31 '26 16:01

Nifle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!