h1

Shell tips 3: Empty and unassigned shell variables

December 4, 2010

The x$foo trick
A shell variable without anything around it will disappear entirely, which can cause syntax errors. This tends to show
up in “if” statements. One, solution, which I dislike , is to write this sort of thing

if [ x$foo = x ]

which is actually a test that the variable is empty, not that is equal to x.

Better is

if [ "$foo" = "" ]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: