0

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
4
  • 1
    Arrays are a ksh/bash/zsh/etc feature not available in sh. But string="item1 item2 item3" isn't an array at all (that would be array=( item1 item2 item3 )), so it's not clear what you're asking for. Commented Jul 21, 2021 at 16:56
  • @Rajeshwerwer : From the error message, it seems to me that your script is not run by bash. Please output echo $BASH_VERSION to check this. Also, you don't have any array in your code, and in the title of your question speaks of a list, not an array. Please be clear in what you want to achieve. Commented Jul 22, 2021 at 9:06
  • @Rajeshwerwer : Thags bash and sh contradict each other. Please remove one of them, and make up your mind, which shell you want to use. Commented Jul 22, 2021 at 9:08
  • You just forgot to add a printf '%s ' in front of r_madoori1/$i. Commented Jul 22, 2021 at 15:09

2 Answers 2

2

Without using any loop you can do this in bash:

Images="alpine ubuntu centos"

Image_tags="r_madoori1/${Images// / r_madoori1\/}"

echo "$Image_tags"

Output:

r_madoori1/alpine r_madoori1/ubuntu r_madoori1/centos
Sign up to request clarification or add additional context in comments.

2 Comments

This is bash-only, correct? Might want to make that explicit since this is also tagged sh.
Thanks, I have added it now. Didn't notice sh tag earlier.
2

The for loop repeats commands; it's doesn't build a list of values. You could write something like

# OK
Image_tags="$(for i in $Images; do
 echo -n "r_madoori1/$i "
done)"

but it would be simpler to perform multiple assignments from the, rather than embed a loop in a single assignment.

# Better
for i in $Images; do
  Image_tags="$Image_tags r_madoori1/$i"
done

If you are using bash, though, you should use real arrays, not space-separated strings.

# Best
Images=(alpine ubuntu centos)
for i in "${Images[@]}"; do
  Image_tags+=( "r_maddori1/$i" )
done

or more concisely

Images=(alpine ubuntu centos)
Image_tags=("${Images[@]/#/r_madoori1/}")

7 Comments

But it is only printing 1st element in array r_maddori1/alpine r_maddori1/alpine r_maddori1/alpine i am expecting r_maddori1/alpine r_maddori1/ubuntu r_maddori1/centos
Did you mistype something? I can't reproduce that.
Images=(alpine ubuntu centos) for i in $Images; do Image_tags="$Image_tags r_madoori1/$i" echo $Image_tags done I tried this
Images=(...) and Images="..." are two different values. You aren't using the real array properly. Please look at the third example carefully.
@CharlesDuffy Indeed, this should be fine : Image_tags=("${Images[@]/#/r_madoori1/}")
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.