Bash For Loop Array: Iterate Through Array Values

last updated in Categories , , , , , , , ,

How do I use bash for loop to iterate thought array values under UNIX / Linux operating systems?

The Bash provides one-dimensional array variables. Any variable may be used as an array; the declare builtin will explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Arrays are indexed using integers and are zero-based.


To declare an array in bash

Declare and an array called array and assign three values:

array=( one two three )

More examples:

files=( "/etc/passwd" "/etc/group" "/etc/hosts" )
limits=( 10, 20, 26, 39, 48)

To print an array use:

printf "%s\n" "${array[@]}"
printf "%s\n" "${files[@]}"
printf "%s\n" "${limits[@]}"

To Iterate Through Array Values

Use for loop syntax as follows:

for i in "${arrayName[@]}"
   # do whatever on $i

$i will hold each item in an array. Here is a sample working script:

# declare an array called array and define 3 vales
array=( one two three )
for i in "${array[@]}"
	echo $i


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

12 comment

  1. Defining the array with double parentheses gave me an error. Using single parentheses did not and worked just fine. This tip mixes them, showing double parentheses at the top, but then using single parentheses in the for loop.

  2. HI
    Want to know , how to create a Two dimension array in bash and print all in one go.
    eg like in one dimension array,
    echo ${array[@]}
    What for two dimension array ??

    Thanks in advance…

    1. Not true, without the quotes you’ll have errors with elements with spaces. Try:

      $ arr=("a 1" "b" "c")
      $ for i in "${arr[@]}"; do echo $i; done
      a 1
      $ for i in ${arr[@]}; do echo $i; done
  3. ist there a nice way of doin this backwards? at the moment im writing something like this:

    for ((i=${#ARRAY[@]}; i > 0; i–)); do echo ${ARRAY[$i]}; done

  4. I tried looping through a directory of approx 80 files. I looped trough them and placed them into an Array. Then I did a nested loop through the array to find duplicated (case insensitive) dir names.

    But this is just as slow as looping straight through the dirs using For x in ./*..

    Can someone with explain this?

  5. A loop is a loop. It is slow.

    It doesn’t matter whether you are looping through array elements or filenames, it’s the same thing.

    Assigning filenames to an array is fast if you can use pathname expansion:

    allfiles=( * )   ## 'shopt -s dotglob' if you want dot files included
    textfiles=( *.txt )

    Merely printing elements of an array doesn’t require a loop:

    printf "%s\n" "${textfiles[@]}"  ## yes, the double quotes ARE necessary
    1. Not true, you can actually still use single quotes.

      printf ‘%s\n’ ‘ ‘${textfiles[@]}’ ‘
      You just have to wrap the variable you’re putting in there in single quotes as well if you choose to use them.

  6. Thank you, I’m a bash noob and this helped me understand loops.

    Maybe you can help me with a challenge I am facing … I am trying to build a script to rsync certain files to a portable drive, and I want to use loop to iterate through file names, each on a new line in the script. The files are all in the same source directory, so I would just like to list the file names of the movies I want on a new line, and have the script copy each to the destination. For example:

    Movie Title 1.mkv
    Another Movie.mkv
    Some Other Title.mkv

    Is there a way I can define an array like that? How do I handle spaces in the file names?

  7. Hi bashnoob..
    Just use a for loop.
    May be try this.. but it depends on the version of the bash you are using..
    You can use folder1 and folder2 in a for loop to check the file system.. it would recognize the spaces and work accordingly with this.
    ls -lh “/$folder1/\”$folder11\””

    Still, have a question? Get help on our forum!