Split Strings in Zsh Without Spawning External Commands
Use zsh parameter expansion flags to split strings into arrays inside the shell, avoiding external pipelines and quoting bugs.
01 Sept 2026, 11:41 UTC

Parsing command output without extra processes
A common pattern in shell scripts is to capture command output, then pipe it through tr, awk, or a while read loop to break it into lines. Each of those tools starts a subprocess, and the quoting around read can silently mangle whitespace or drop empty lines. If you are already using zsh, you can usually do the splitting inside the shell with parameter expansion flags.
The flags that split scalars into arrays
Zsh treats a scalar variable as a single string. Parameter expansion flags tell the shell to expand that string into an array instead. The most useful flags for splitting are:
${(f)var}— splits on newlines into separate array elements${(z)var}— splits using shell word-splitting rules, honoring quotes${(s::)var}— splits on an arbitrary single-character delimiter placed between the colons
These are core zsh syntax, not plugins, and they are present in zsh 5.x and earlier releases. Because the expansion happens inside the shell, you avoid the overhead and failure modes of external pipelines.
| Flag | Splits on | Best for |
|---|---|---|
(f) | Newlines | Line-by-line command output |
(z) | Shell word rules (IFS, quotes) | Command-line-like strings |
(s::) | Single custom character | CSV or delimiter-specific lists |
Worked example: turning command output into a safe array
Suppose a command produces newline-separated output and you want to inspect each line individually. Instead of piping to while read, capture the output into a scalar and expand it with (f).
# Run in a terminal with zsh as your interactive shell.
# No special permissions are required.
output=$(some-command)
# Split on newlines. Each original line becomes one array element.
lines=("${(f)output}")
# Print each element on its own line to verify the split.
print -rl -- "${lines[@]}"
Replace some-command with the command you are parsing. The -- after print protects against arguments that begin with a dash. If the command output contains blank lines, those blank lines become empty array elements rather than being skipped.
To verify the result, run zsh --version to confirm you are using zsh, then try the snippet with printf 'apple\n\nbanana\n' as the command output. The expansion should preserve the blank line as an empty array element. Inspect the count with ${#lines[@]} if an element appears to be missing.
When word splitting is safer than line splitting
If the text contains quoted phrases or shell-like escaping, ${(z)var} applies the same rules zsh uses for command lines. That means quoted sections stay together even if they contain the delimiter character. Use ${(s:,:)var} when you need to split on a character other than newline, such as commas in a list.
Limitations and what to check
These flags are not a drop-in replacement for bash arrays. Bash does not support ${(f)var}, so scripts using them are zsh-only. Also, ${(z)var} still honors IFS and performs quote removal, so untrusted input containing embedded quotes can behave unexpectedly. Empty elements are preserved by (f) and (s::), which differs from tools like tr or awk that may discard blank lines.
Practical check: after expanding a variable, print each element with print -rl -- "${lines[@]}" and compare the count against ${#lines[@]}. If an element looks missing, it may be an empty element rather than a dropped line.
Use the shell, not the pipeline
For zsh scripts that parse command output line by line, parameter expansion flags reduce subprocess count and quoting bugs. Start with ${(f)var} for newline splitting, verify with print -rl, and reach for ${(z)var} or ${(s::)var} when the delimiter or quoting rules require it.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.