Tuesday, 16 June 2020

Loop through an array of strings in Bash?

You can use it like this:
## declare an array variable
declare -a arr=("element1" "element2" "element3")

## now loop through the above array
for i in "${arr[@]}"
do
   echo "$i"
   # or do whatever with individual element of the array
done

# You can access them using echo "${arr[0]}", "${arr[1]}" also
Also works for multi-line array declaration
declare -a arr=("element1" 
                "element2" "element3"
                "element4"

from : https://stackoverflow.com/questions/8880603/loop-through-an-array-of-strings-in-bash

No comments:

Post a Comment

How to check if a file contains a specific string using Bash

 In case if you want to check whether file does not contain a specific string, you can do it as follows. if ! grep -q SomeString "$File...