3

I am doing this script (with zsh, but I guess that it is not very important):

mylist=`seq 1 3`

for i in $mylist ; do echo "abc/$i" ; done

This gives :

abc/1

2

3

while I would like to see :

abc/1

abc/2

abc/3

A huge thanks to somebody that may find why it does not work/how to do.

Kusalananda
  • 333,661

3 Answers3

5

You can keep it simple:

for i in 1 2 3; do echo "abc/$i" ; done

OR

for i in $(seq 1 3); echo "abc/$i"

Output:

abc/1
abc/2
abc/3
1

Another way to do this kinda thing is via brace expansion:

echo abc/{1..3}

But naturally, since you asked for newlines, you'd need to do it like this:

abc_strings=( abc/{1..3} )
printf "%s\n" "${abc_strings[@]}"
rm-vanda
  • 277
0

Just tried this in an online Zsh :

for i in `seq 1 3` 
do
echo "abc/$i\n"
done

Got the following:

abc/1

abc/2

abc/3

  • ah yes, thanks. But if you try with a variable, this creates a problem. – Mathieu Krisztian Jan 10 '21 at 20:39
  • In this case one does not need to create a variable, at least not in the example you have given. Defining it through the ticks (``) should suffice. – Maximus Superbus Jan 10 '21 at 20:42
  • (i had changed my message meanwhile) – Mathieu Krisztian Jan 10 '21 at 20:43
  • "this" is meaning the message of my question (for which there was a solution). There are problem with the editors : they don't print some characters. Anyway, see in my message : the inittial attempt was not working. mylist=seq 1 3 for i in $mylist ; do echo "abc/$i" ; done This gives : abc/1 2 3 – Mathieu Krisztian Jan 10 '21 at 20:44