Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: variables not working as expected



2009-07-24 11:36:35 -0700, spiralofhope:
> This works in bash, dash, ash and sash, but not in zsh:
> 
> a="echo foo bar"
> $a

That behavior of those shells, inherited from the Bourne shell
is the cause of half the bugs and especially the security ones
in existing shell scripts. It's been fixed in zsh.

See http://zsh.dotsrc.org/FAQ/zshfaq03.html#l18

Do

$=a

to explicitly request the word splitting. The equivalent of the
other shells would be $=~a, that is request both word spitting
and filename generation.

But chances are that you actually want:

eval $a

(eval "$a" is other shells)

if you want the content of $a to be interpreted as a command
line and not as a list of arguments to a simple command.

setopt shwordsplit  globsubst

enables the Bourne shell behavior. Same with "emulate sh", if
wou're really keen on shooting yourself in the feet ;)

Note: to get zsh's behavior in other shells, you'd do:

IFS=; set -f

But that would also disable filename generation everywhere,
not only upon variable expansion.

Also note that zsh performs word splitting but not filename
generation upon command substitution $(cmd) or `cmd` (and also
the trimming of every trailing character, a bogus behavior of
the Bourne shell which it did not fix).

Another thing to note (IMO another bug it did not fix) is that
$a expands to no argument instead of en empty argument when $a
is empty just like the other shells, which makes so that even in
zsh, it's generally safer to use quotes anyway just as in the
other shells.

-- 
Stephane



Messages sorted by: Reverse Date, Date, Thread, Author