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:
- [WayBack] Bash eval replacement $() not always equivalent? – Stack Overflow
- [WayBack] linux – Why should eval be avoided in Bash, and what should I use instead? – Stack Overflow
- [WayBack] Bash Script – Variable content as a command to run – Stack Overflow
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]}" alsoAlso works for multi-line array declaration
declare -a arr=("element1" "element2" "element3" "element4" )
–jeroen






Leave a comment