Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: variables not working as expected
- X-seq: zsh-users 14255
- From: Stephane Chazelas <stephane_chazelas@xxxxxxxx>
- To: spiralofhope <spiralofhope@xxxxxxxxx>
- Subject: Re: variables not working as expected
- Date: Fri, 24 Jul 2009 20:01:03 +0100
- Cc: zsh-users@xxxxxxxxxx
- Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.fr; h=Received:X-Yahoo-SMTP:X-YMail-OSG:X-Yahoo-Newman-Property:Date:From:To:Cc:Subject:Message-ID:Mail-Followup-To:References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To:User-Agent; b=gyIVpXokYg5PqejVsUaoBNzugEjb4WyMgLURW9U1LINkEy6jxPSDPLA9zzB9n8Nd20amSuhVfg96Ze/81FQrYlEEonETLgUYtSgSVsPmOb3tdTZf+M9Q8bYogNiG14AKsypMpqBdz1KunaHKAEhvPcSe3EBDf8DXMTSXuqhKsY0= ;
- In-reply-to: <20090724113635.796ef7b4@xxxxxxxxx>
- Mail-followup-to: spiralofhope <spiralofhope@xxxxxxxxx>, zsh-users@xxxxxxxxxx
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
- References: <20090724113635.796ef7b4@xxxxxxxxx>
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