Home > front end >  Make ZSH pass a variable as space-delimited parameters
Make ZSH pass a variable as space-delimited parameters

Time:11-29

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.

CodePudding user response:

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.

  • Related