2

OS: Monetary (12.0.1)
default shell: zsh (5.8)

#!/bin/zsh

LINE="I:would:like:coffee."
IFS=:
set $LINE

echo $1
echo $2
echo $3
echo $4

exit 0
./script.sh
zsh ./script.sh

Both ways of running the script returned

I:would:like:coffee.

Only when running in bash, did the script work as expected

bash ./script.sh
I
would
like
coffee.

I do want to know that the reason why IFS works as expected in bash, not in zsh.

  • 1
    Does this answer your question? [zsh is not splitting by IFS after parameter expansion](https://stackoverflow.com/q/46313940/7939871) – Léa Gris Nov 23 '21 at 06:48
  • Short answer: zsh does not not provide POSIX-shell compatibility in its default operation mode. Or duplicate of https://stackoverflow.com/q/46313940/7939871 – Léa Gris Nov 23 '21 at 06:50
  • @LéaGris The link exactly answered what I was wondering, thanks. – Animal Farm Nov 23 '21 at 07:15
  • In the zsh-version of your script, do a `set ${(z)LINE}` instead. However,what's the point in using `set`? Wouldn't it make more sense to create an explicit array of the words in `LINE`, and work with the array? – user1934428 Nov 23 '21 at 09:30
  • For zsh, there is no need to change IFS. You can do a `linearr=(${(@s,:,)LINE})`. After this, i.e. `echo $linearr[2]` prints _would_, since this is the second part of your string. Search [here](https://zsh.sourceforge.io/Doc/Release/Expansion.html#Parameter-Expansion) for the term _field splitting_ to understand why this works. – user1934428 Nov 23 '21 at 09:38
  • `IFS` works exactly as expected in both shells. The difference is that in `zsh`, an unquoted parameter expansion is *not* (by default) subject to word-splitting, which is what `IFS` affects. – chepner Nov 25 '21 at 17:59

1 Answers1

2

It looks like ZSH feature. See on ZSH FAQs. For only one script you can use -y flag.

zsh -y ./script.sh
irvis
  • 136
  • 1
  • 6
  • 1
    Don't forget to also check possible duplicates like: [zsh is not splitting by IFS after parameter expansion](https://stackoverflow.com/q/46313940/7939871) – Léa Gris Nov 23 '21 at 06:55