[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

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
>     0000000
>     $ [ -n "$undefinedString" ]  &&  printf "$undefinedString" | od -c
>     $ [ -z "$undefinedString" ]  &&  printf "$undefinedString" | od -c
>     0000000
> 
> 
> 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
http://tldp.org/LDP/Bash-Beginners-Guide/html/Bash-Beginners-Guide.html

Advanced Bash Scripting Guide
http://tldp.org/LDP/abs/html/


Reply to: