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.

Suppose I have the following initialization of bash array:

my_array=(
    "/usr/bin"
    "/usr/lib/*.so"
)

If I do iteration using:

for array_item in ${my_array[@]} 
do 
    ... 
done 

Then the content of the my_array is expanded containing path to any files with .so extension in /usr/lib/ directories. but I just wanted the array contain two string which is "/usr/bin" and "/usr/lib/*.so".

How should I do that?

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted
for array_item in "${my_array[@]}"
do 
    ...
done

or

for idx in $(seq ${#my_array})
do
    array_item="${my_array[$idx]}"
done
share|improve this answer
    
no, its not working, the file list still expanded. –  uray Sep 18 '10 at 22:48
    
The first one works for me in bash 3.2.48 and both in zsh 4.3.9. –  Maciej Piechotka Sep 18 '10 at 22:59
    
oh ok, my fault I should echo using quotes for array_item (I use bash 4.1.7 btw) –  uray Sep 18 '10 at 23:22
    
Good to hear it. I'm on "wrong" system so I needed to test it on shell server - hence not brand new shell ;) –  Maciej Piechotka Sep 18 '10 at 23:56
2  
I think for idx in ${!a[@]} is preferable to using seq since it avoids unnecessarily using an external program and works even if the array is sparse. –  Dennis Williamson Sep 19 '10 at 4:04
add comment

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.