Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to use arrays in my sh script.

My target is to create an array with the values a b c and print all values in the array.

I succeeded to print each array, but I failed to print all values in the array.

Following example:

Set each value in arr:

n=1
eval arr$n=a
n=2
eval arr$n=b
n=3
eval arr$n=c

Print each value from arr:

n=1
eval echo \$arr$n
a
n=2
eval echo \$arr$n
b
n=3
eval echo \$arr$n
c

Now I want to print all values in $arr and instead of a b c I get:

n="*"
eval echo \$arr$n
{*}*

The values should be a b c.

share|improve this question
    
check this question unix.stackexchange.com/questions/137566/… –  Nischay Jul 3 at 7:51
    
When you are limited to /bin/sh try avoiding too much scripting. How much colleagues will understand your eval-calls? You do want to go on vacation some day. –  Walter A Jul 3 at 13:43

2 Answers 2

up vote 4 down vote accepted

sh does not support array, and your code does not create an array. It created three variable arr1, arr2, arr3.

To initialize an array element in a ksh-like shell, you must use syntax array[index]=value. To get all element in array, use ${array[*]} or ${array[@]}.

Try:

n=1
eval arr[$n]=a
n=2
eval arr[$n]=b
n=3
eval arr[$n]=c

n=1
eval echo \${arr[$n]}
n=2
eval echo \${arr[$n]}
n=3
eval echo \${arr[$n]}

n='*'

eval echo \${arr[$n]}
share|improve this answer
    
I must to use sh script ) I dont have ksh/bash ) on my box machine , so ksh isnt relevant sorry –  maihabunash Jul 3 at 7:54
    
sh does not support array! –  Gnouc Jul 3 at 7:55
    
Is doing eval echo \${arr[$n]} not the same as doing ${arr[$n]} ? –  Emmanuel Jul 3 at 7:57
    
It's different. echo ${arr[$n]} causes syntax error. –  Gnouc Jul 3 at 8:04
    
But you can use arr[$n]=a without eval. –  Scott Jul 3 at 16:24

I agree that the correct answer is to find a different approach.  Get a better shell, switch to some other scripting language (maybe perl or python), or rethink your design.  But, to get a list of the values of all the variables whose names begin with arr, you can use

set | sed -n '/^arr[^=]*=/s///p'

This will list them on separate lines.  To get them all on one line, use

echo $(set | sed -n '/^arr[^=]*=/s///p')

Not that this will sort them in lexicographical order, so arr10arr19 will appear between arr1 and arr2.  (The same thing happens if you have files named maih1, maih2, …, maih10, …, maih19.)  If you know in advance how many array elements you are going to have, you can fix this by using leading zeroes; e.g., arr01. arr02, …, will get you up to 99.

P.S. The trick with set | sed … will fail if any of the variables’ values contain newline(s).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.