0

I'm on zsh. What is going on here...?

1

It seems I just can't set the IFS; it stays on '\n' (newline, not 'n') when I investigate, but neither in my .zshenv nor .zshrc did I set it.

Any suggestions? Thanks in advance!

EDIT

Another example of how weird my arrays are behaving: 2

Again, thanks in advance!

  • 1
    Try `b=($=a)` - `zsh` doesn't perform word splitting on variable expansions by default - see for example [What is word splitting? Why is it important in shell programming?](https://unix.stackexchange.com/a/26672/65304) – steeldriver Jan 11 '22 at 23:42
  • That worked! So weird, it always worked for me like above until today :/ – René Fokkema Jan 12 '22 at 00:12
  • 4
    please do not post pictures of text ... post the text itself ... some people here cannot view images – jsotola Jan 12 '22 at 02:41

1 Answers1

4

The issue is not that you can't set IFS, it's that (unlike in bash for example) in zsh, unquoted variable expansions are not subject to "split + glob" (word splitting and filename generation). So the whole of $a is being assigned to the first element of b (which is $b[1], since arrays are indexed from 1 in zsh - again different from bash).

To get bash-like behavior, you can either set zsh's shwordsplit shell option, or make the variable expansion word split explicitly using $=a in place of plain $a.

For a more nuanced explanation, see What is word splitting? Why is it important in shell programming?

steeldriver
  • 78,509
  • 12
  • 109
  • 152