Thursday, December 18, 2008

Join two arrays in bash script


I already discussed about bash array in one of my older post . Here is a simple bash script to show how we can join two arrays field by field.

$ cat combarr.sh

#!/bin/sh

COUNTRY=( "Japan" "China" "India")
CAPITAL=( "Tokyo" "Beijing" "New Delhi")

n=${#COUNTRY[@]} #num elements in array COUNTRY

for (( i=0; i<n; i++ ))
do
COMB[$i]=${COUNTRY[$i]}"["${CAPITAL[$i]}"]"
echo ${COMB[$i]}
done

Executing:

$ ./combarr.sh
Japan[Tokyo]
China[Beijing]
India[New Delhi]

Related post:
- Introduction to bash array
- Combine array in bash

2 comments:

j said...

this is awesome, thanks!

Anonymous said...

This is brilliant. Thank you.

© Jadu Saikia www.UNIXCL.com