I am trying to create a new array using for loop
Images="alpine ubuntu centos"
Image_tags="$(for i in $Images; do
r_madoori1/$i
done)"
echo $Image_tags
I am expecting
Image_tags="r_madoori1/alpine r_madoori1/ubuntu r_madoori1/centos"
instead i am getting below error
./shell.sh: line 7: r_madoori1/alpine: No such file or directory
./shell.sh: line 7: r_madoori1/ubuntu: No such file or directory
./shell.sh: line 7: r_madoori1/centos: No such file or directory
2
Answers
The
for
loop repeats commands; it’s doesn’t build a list of values. You could write something likebut it would be simpler to perform multiple assignments from the, rather than embed a loop in a single assignment.
If you are using
bash
, though, you should use real arrays, not space-separated strings.or more concisely
Without using any loop you can do this in
bash
:Output: