I know that Bash has word splitting but zsh doesn't, and I'm not familiar with others(csh, tcsh, ksh, etc), but I was wondering if it is a part of any standard.

In other words, does sh have word splitting, or is it a Bash-only feature? If I wanted to write a portable shell script, would I have to account for word splitting, or is it something nonstandard that is added by other shells?

  • zsh has word splitting as well but it's not done implicitly (unless zsh runs in sh/bash/ksh emulation), you have to request it: $=var instead of $var. Same for globbing upon expansions, another thing that is done implicitly by POSIX shells and is about as dangerous/unwanted as word splitting: $~var instead of $var. So $var in POSIX shells is like $=~var in zsh. – Stéphane Chazelas Jan 23 '18 at 20:12

Implicit word splitting, i.e. word splitting on an unquoted variable expansion ($foo as opposed to "$foo"), is something that all POSIX compliant shells do, and more generally all sh shells. They also perform globbing on the result. This is why you need double quotes around variable substitutions. The same goes for command substitutions.

POSIX calls these field splitting and pathname expansion.

Zsh deviates from the standard sh behavior. It doesn't perform word splitting on unquoted variable substitutions (but it does perform word splitting on unquoted command substitutions), and it doesn't perform globbing on unquoted substitutions at all. (Zsh has those features, of course, but they're explicit: $=foo to do word splitting and $~foo to do globbing.) Zsh is not an sh-compatible shell; it's fairly close, but not compatible, and the reduced implicit splitting is one of the main deviations. Zsh has a compatibility mode (which is entered automatically if the zsh executable is called sh or ksh) in which it does perform implicit word splitting and globbing like sh, among other things.

Bash and ksh are both sh-compatible shells. Bash does have a few incompatibilities with POSIX, but you have to dig a lot deeper to find them. On important issues like implicit splitting, it's compatible.

(T)csh is a completely different family of shells. Its syntax is vastly different from sh. It's also pretty much dead, so don't worry about it.

  • Thanks for the detailed explanation of all the different shells, this is exactly what I was looking for. – 3p1k5auc3 Jan 24 '18 at 16:33

Field splitting is in the POSIX standard: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05

After parameter expansion (Parameter Expansion), command substitution (Command Substitution), and arithmetic expansion (Arithmetic Expansion), the shell shall scan the results of expansions and substitutions that did not occur in double-quotes for field splitting and multiple fields can result.

The shell shall treat each character of the IFS as a delimiter and use the delimiters as field terminators to split the results of parameter expansion, command substitution, and arithmetic expansion into fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.