1

I tried this code in my shell script in ubuntu 12.04 bash

IFS=$'\t'
name=(one two three four five)
fam=($(seq -s" " 1 1 5))
for (i=0;i<5;i++) 
do 
  printf "%s\t%s\n" ${fam[i]} ${name[i]} 
done

The output I want is like that

1 one
2 two
3 three
4 four
5 five

But the really output is

1 2 3 4 5   one
two 
three   
four    
five

What went wrong in my code? and how to print more than one array variable in a line just using one loop?

2 Answers 2

2
name=(one two three four five)
fam=(1 2 3 4 5)

for i in ${!name[*]}
do
  printf '%s %s\n' ${fam[i]} ${name[i]}
done

or you can just fix

for  (i = 0; i < 5; i++)
for ((i = 0; i < 5; i++))
Sign up to request clarification or add additional context in comments.

1 Comment

Because if I want to get a big sequence like 10000,it is not a good way input all numbers by keyboard?What do you think?
0

When you set IFS to '\t' it causes the array initializer to break the input on tabs so you get '1 2 3 4 5' assigned to fam[0]. You need to change the seq delimiter to match.

fam=($(seq -s $'\t' 1 1 5))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.