How do I inline an array of strings in a bash for loop ? This works:
array=(one two) for i in ${array[*]};do echo $i; done
But I'd like to eliminate the extra local variable. I've tried many variations that seem reasonable, for example:
for i in ${("one" "two")[*]};do echo $i; done
or
for i in ${"one" "two"};do echo $i; done
In each case, it treats one
and two
as commands :(
There are two ways to iterate over items of array using For loop. The first way is to use the syntax of For loop where the For loop iterates for each element in the array. The second way is to use the For loop that iterates from index=0 to index=array length and access the array element using index in each iteration.
Create a bash file named 'for_list1.sh' and add the following script. A string value with spaces is used within for loop. By default, string value is separated by space. For loop will split the string into words and print each word by adding a newline.
bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.
Did you try with:
for i in "one" "two"; do echo "$i"; done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With