Re: Expanding undefined variables within a shell
On Sat, Sep 17, 2011 at 09:11:16AM -0700, Regid Ichira wrote:
> Within a shell, what is the difference between [ -n undefinedString ] and [ -n "$undefinedString" ] ?
> With bash I get:
> $ unset undefinedString
> $ [ -n undefinedString ] && printf "$undefinedString" | od -c
> $ [ -n "$undefinedString" ] && printf "$undefinedString" | od -c
> $ [ -z "$undefinedString" ] && printf "$undefinedString" | od -c
> I mean, shouldn't [ -n undefinedString ], which I guess is without shell expansion, give an error? Clearly
> it is an empty string.
Actually, "undefinedString" *is* a string. However, "$undefinedString" is a variable that doesn't reference anything, so the above is behaving correctly. (Assuming you're using bash, which I probably shouldn't.)
I would highly recommend lurking in comp.unix.shell, and searching the archives in GoogleGroups. It also wouldn't be a bad idea to bookmark the following pages:
Bash Guide for Beginners
Advanced Bash Scripting Guide