Because zsh doesn't do word splitting to parameter expansions by default, unlike POSIXy shells. It does for command substitutions, so the round trip via one "fixes" it for you. But it's still far from the right thing to do in zsh, and the combination of echo
and command substitution could even mangle the data in some cases.
In zsh, you could use $=PATH
to ask for word splitting, or use the more proper solutions presented here: How to split a string by ':' character in bash/zsh?
Also in case of PATH
, it's tied to the array variable path
in zsh by default, so you don't need to do anything to access it as an array.
(In IFS=: p=($PATH)
, the unquoted expansion would also be subject to filename generation or globbing, so if any of the paths in PATH
happen to contain globbing characters (*?[]
, maybe others), the results may not be what you want. Round-tripping via a command substitution and echo
would also remove any trailing newlines, and (depending on echo
) might also mangle backslashes.)
$PATH
as an array, you can use$path
in zsh, see zsh.sourceforge.io/Doc/Release/Parameters.html (search for "path <S> <Z> (PATH <S>)").$PATH
variable or something else likefoo='a:b c:d'
(should yield 3 items); if you feel like writing an answer, that would be great :)