Home > database >  How to do multi-word brace expansion for command-line flags
How to do multi-word brace expansion for command-line flags

Time:10-01

If you have a command that takes an argument with a value often you can do something like:

journalctl -u{rspamd,postfix}
# Expands to: journalctl -urspamd -upostfix


journalctl --unit={rspamd,postfix}
# Expands to: journalctl --unit=rspamd --unit=postfix

However some commands don't support arguments concatenated like that and require --unit rspamd --unit postfix. Is there a clean way to expand multiple values of these flag using brace expansion? Obviously the following doesn't work:

journalctl --unit {rspamd,postfix}
# Expands to: journalctl --unit rspamd postfix

CodePudding user response:

As long as the additional arguments don't themselves contain whitespace, you can do something like

journalctl ${(z):---unit {rspamd,postfix}}

The parameter expansion uses the z flag to perform word-splitting on the result of the parameter expansion. The brace expansion includes the space in the brace expansion because the expression following :- is not subject to word-splitting. As a result, the entire parameter expansion (which, yes, doesn't actually involve a parameter) produces 4 words: --unit, rspamd, --unit, and postfix.

CodePudding user response:

How about quote the space character with print -z?

% print -z journalctl --unit' '{rspamd,postfix}
% journalctl --unit rspamd --unit postfix

Or even escape it with \:

% print -z journalctl --unit\ {rspamd,postfix}
% journalctl --unit rspamd --unit postfix

print -z:

-z

Push the arguments onto the editing buffer stack, separated by spaces.

--- zshbultins(1), zsh built-in command, print

  • Related