Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Suppose I have the following initialization of bash array:


If I do iteration using:

for array_item in ${my_array[@]} 

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
up vote 5 down vote accepted
for array_item in "${my_array[@]}"


for idx in $(seq ${#my_array})
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
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

Your Answer


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.