[SOLVED] troubles with bash script tests "-z" and "-n"
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
(blush) I should be able to sort this out, but I'm stumped.
Here are some commands and the results:
Code:
stuff="xxx xxx" # the blank is important
echo $stuff
xxx xxx
if [ -z stuff ]; then echo nothing; fi # no output
if [ -n stuff ]; then echo non-zero; fi # output
non-zero
Code:
stuff=""
echo "yyy $stuff yyy" # output has two blanks -- check
yyy yyy
if [ -z stuff ]; then echo nothing; fi # no output ???
if [ -n stuff ]; then echo non-zero; fi # output ???
non-zero
The bash man-page says:
-z string -- true if length of string is zero
-n string -- true if length of string is non-zero
I thought that stuff="" creates a string of length==0.
There is something obvious here that I am obviously missing.
I'll take my lumps, but I left my flame-suit at the home.
~~~ 0;-Dan
Last edited by SaintDanBert; 11-04-2010 at 06:26 PM.
Since the solution involved quoting within scripts, I offer the following from the man bash manual page:
Code:
Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.
Enclosing characters in double quotes preserves the literal value of
all characters within the quotes, with the exception of $, `, \,
and, when history expansion is enabled, !. The characters $ and `
retain their special meaning within double quotes. The backslash
retains its special meaning only when followed by one of the
following characters: $, `, ", \, or <newline>. A double quote may
be quoted within double quotes by preceding it with a backslash.
If enabled, history expansion will be performed unless an ! appearing in double quotes is escaped using a backslash.
The backslash preceding the ! is not removed.
Please don't re-open old threads for trivial purposes. Especially since you (!) already said exactly the same thing two and a half years ago, in the second post.
This is a classic case of bash pitfall #4, by the way.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.