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.

8
  • 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? Jan 9, 2021 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 ? Jan 9, 2021 at 21:13
  • 1
    As mentioned in the accepted answer to the Q&A linked above, 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) Jan 9, 2021 at 21:37
  • 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, 2021 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, 2021 at 12:49

3 Answers 3

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
3
  • sure thank you, but it was just an example : I really would like to use the "result of a unix commant with ` ... `, not to cheat by putting by hand each value. Jan 9, 2021 at 21:10
  • excellent. Thanks a lot Bruce. Unfortunately, I'm new, else I would have given you at least 1000 points for your help. Jan 9, 2021 at 21:48
  • Thank you @MathieuKrisztian Jan 9, 2021 at 22:59
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[@]}"
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
4
  • ah yes, thanks. But if you try with a variable, this creates a problem. Jan 10, 2021 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. Jan 10, 2021 at 20:42
  • (i had changed my message meanwhile) Jan 10, 2021 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 Jan 10, 2021 at 20:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .