Reputation: 3010
I would like to create an alias that works in zsh and bash, but weirdly enough zsh tries to pass on an unquoted variable as a single argument:
❯ zsh -f
grasshopper% LS_OPTIONS="-l -a" && ls $LS_OPTIONS
ls: invalid option -- ' '
Try 'ls --help' for more information.
grasshopper% bash --norc
bash-5.1$ LS_OPTIONS="-l -a" && ls $LS_OPTIONS
total 0
drwxr-xr-x 2 janekf janekf 40 28. Nov 14:02 .
drwxrwxrwt 24 root root 2400 28. Nov 16:41 ..
Is there a option to stop that?
See also Using variables as command arguments in zsh, but the answer there is zsh-specific.
Upvotes: 2
Views: 643
Reputation: 22366
Your LS_OPTIONS will be passed as a single parameter to ls
, and of course ls
can not handle it.
In zsh, define your alias as
alias ls='ls ${(z)LS_OPTIONS}'
The (z) causes the value to be split on white space and passed as individual arguments to ls
.
Upvotes: 2
Reputation: 532418
If you want something that works in both zsh
and bash
, use an array.
LS_OPTIONS=(-l -a) && ls "${LS_OPTIONS[@]}"
You can set the SH_WORD_SPLIT
option in zsh
to make parameter expansions undergo word-splitting just as in bash
, but unquoted expansions in bash
are subject to pathname expansion as well, so I don't recommend their use.
Upvotes: 5