The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,858 other subscribers

One day I will dig in the various ways that bash can do evaluation, for now: there is eval, “ and $() and I’m not sure when to choose which.

Posted by jpluimers on 2020/06/10

A while ago, I had to execute a series of aliases of which the names were stored in an array. A simple for loop with en eval call did the job.

Then I found out there are at least two more ways of evaluation in bash, so here are just a few links giving me a head start if I ever dig this up again:

Note that looping over parameters is different than over an array: [WayBack] Loop through an array of strings in Bash? – Stack Overflow

ou 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"
                )

–jeroen

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.