Re: Expanding undefined variables within a shell
Regid Ichira <regid23@yahoo.com> 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
You are just testing for the length of "undefinedString" here, which
is a perfectly good string even without the quotation marks. When you
then do printf "$undefinedString", the variable is actually looked
up, found to be undefined and "$undefinedString" evaluates to "".
> $ [ -n "$undefinedString" ] && printf "$undefinedString" | od -c
Here, the variable is evaluated to "" (as above) and then checked for
nonzeroity. Obviously, that is false.
> $ [ -z "$undefinedString" ] && printf "$undefinedString" | od -c
> 0000000
Same as above, but checked if the string is empty. Obviously, it is.
>I mean, shouldn't [ -n undefinedString ], which I guess is without shell expansion, give an error? Clearly
>it is an empty string.
No, it is the string undefinedString, which has 15 letters (if I
didn’t miscount…).
> I think I am confusing various terms. An explanation, perhaps by using the concept of C like strings,
>might be helpful.
Bash not only knows numerical constants like C (1, 5, 42) but also
string constants which per se don’t require further mark up, unlike
C. "" is only needed in Bash if there are spaces in the string.
C | Bash
1 | 1
"string" | "string"
"string" | string
"stri ng" | "stri ng"
"stri" "ng" | stri ng (two seperate strings)
Best regards,
Claudius
--
The speed of anything depends on the flow of everything.
Please use GPG: ECB0C2C7 4A4C4046 446ADF86 C08112E5 D72CDBA4
http://chubig.net/ http://nightfall.org
Reply to: