if [-n ]


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if [-n ]
# 1  
Old 07-26-2011
if [-n ]

I read about bash script if options:
Quote:
Sometimes, it is a good idea to protect variable names in double quotes. This is usually the most important if your variables value either (a) contains spaces or (b) is the empty string. An example is as follows:
#!/bin/bash X="" if [ -n $X ]; then # -n tests to see if the argument is non empty echo "the variable X is not the empty string" fi
This script will give the following output:
the variable X is not the empty string
Why ? because the shell expands $X to the empty string. The expression [ -n ] returns true (since it is not provided with an argument). A better script would have been:
#!/bin/bash X="" if [ -n "$X" ]; then # -n tests to see if the argument is non empty echo "the variable X is not the empty string" fi
In this example, the expression expands to [ -n "" ] which returns false, since the string enclosed in inverted commas is clearly empty.
http://www.panix.com/~elflord/unix/bash-tute.html
I still don't understand what exactly -n mean, can someone explain to me in it own word what exactly it check?

Thank
# 2  
Old 07-26-2011
Man Page for test (OpenSolaris Section 1) - The UNIX and Linux Forums
-n string True if the length of string is non-zero.
# 3  
Old 07-26-2011
From man test
Quote:
-n STRING
the length of STRING is nonzero
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question