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
  • 320,670
  • 36
  • 633
  • 936
  • 7
    *"with zsh, but I guess that it is not very important"* in fact, it's very important. See for example [zsh: What is word splitting? Why is it important in shell programming?](https://unix.stackexchange.com/questions/26661/what-is-word-splitting-why-is-it-important-in-shell-programming) – steeldriver Jan 09 '21 at 20:48
  • @steedriver : you have a good point. Indeed, with bash, it works directly. Is there a way to make it work with zsh ? – Mathieu Krisztian Jan 09 '21 at 21:13
  • 1
    As mentioned in [the accepted answer to the Q&A linked above](https://unix.stackexchange.com/a/26672/65304), you should consider using an array ex. `mylist=( $(seq 1 3) )` (you can use backticks in place of `$(...)` for the command substitution if you prefer, however it is considered deprecated) – steeldriver Jan 09 '21 at 21:37
  • excellent, it works. thank you – Mathieu Krisztian Jan 09 '21 at 21:48
  • ok got it. zsh does not split in general per design. very confusing – alecxs Jan 09 '21 at 22:41
  • 1
    Note that you can also force the execution of your script to be in a POSIX-compatible fashion using the shebang line `#! /bin/sh` at the top of the file. Depending on your system this may run ksh/zsh/bash/dash/ash, but in any case in a mode that disables such features as the one which is causing this behavior. N.B. on most systems /bin/sh is not *completely* POSIX compatible, but usually good enough. – ljrk Jan 10 '21 at 12:09
  • 1
    Quickest change to make it work with zsh could also be changing `$mylist` to `${=mylist}`. That enables word splitting on the variable expansion. – JoL Jan 10 '21 at 12:49
  • thanks both of you – Mathieu Krisztian Jan 10 '21 at 12:50

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
Bruce Malaudzi
  • 1,522
  • 1
  • 4
  • 11
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
  • 1
  • 2
  • 10
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