betweenGo

Save the Output in a Shell Script Array

by Frank Kim on Dec.14, 2007, under Bash

I wanted to do some shell processing on the contents of a directory. To do this I needed to put the contents of the directory in an array. Fortunately this is not difficult with bash.

  1. First capture the output.

    ls_output=$(ls /images)

  2. Next declare an array and reference the output with this array.

    declare -a img_files
    img_files=($ls_output)

  3. Now you can access the array directly or loop through it.

    echo ${img_files[0]}
    for img_file in ${img_files[@]}
    do
      ...
    done

Thanks O’Reilly’s Bash Cookbook.


  • Share/Bookmark

Related posts:

  1. Save the Output in a Shell Script Variable
  2. Unable to start Cygwin shell from Windows command prompt
  3. Moving Your Cygwin Installation
  4. Printing out an array
  5. How to Set an Array Property in an ATG Form


Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!