Bash Shell Scripting

How to iterate through an array in a Bash Shell Script

Written by Ian Carnaghan · 19 sec read >
  • Create a new Bash Shell Script eg. loopThroughArray.sh
  • Open the Script with an Editor eg. GEdit
  • Add the array definition through which the script should iterate
    array=( one two three four five )
  • The expression ${array[@]} contains all values within the array
    ${array[@]}
  • Now we create a for loop to iterate through the array
     
    for num in ${array[@]}
     
    do
     
    echo $num
     
    done
     
    
  • Once the Script executes, all values within the array be be echoed to the screen.

    To extend the Script additional statements can be added to the body of the for loop

Last Updated On April 07, 2018
Written by Ian Carnaghan
I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment. In my spare time I teach undergraduate classes in web development. Profile

Leave a Reply

avatar
  Subscribe  
Notify of