15

I am fixing some old bash scripts I often see

if [[ -n $VARIABLE ]]; then 

syntax I tried to google it but could find why "-n" is used for, following is what I know

Comparisons:
  -eq   equal to
  -ne   not equal to
  -lt   less than
  -le   less than or equal to
  -gt   greater than
  -ge   greater than or equal to

File Operations:

  -s    file exists and is not empty
  -f    file exists and is not a directory
  -d    directory exists
  -x    file is executable
  -w    file is writable
  -r    file is readable

would anyone let me know what -n do ?

3 Answers 3

22

help test would tell you:

String operators:

  ....

  -n STRING
     STRING      True if string is not empty.
Sign up to request clarification or add additional context in comments.

12 Comments

many Thanks i am pretty new to scripting and even while googling i only searched for if syntax which didnt give me what i was looking for.
@NoviceCoder: That's because it's not if syntax.
@IgnacioVazquez-Abrams Well, there is an if syntax: if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi See tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html.
@RaduRădeanu That doesn't make it if syntax; it's an operator for the test ([) command.
@RaduRădeanu The if command takes a test command. It's the test command (or [) that has the -n operator.
|
5

The various tests that [[ ... ]] and [ ... ] use in if and while loops are from the Unix test command itself. An easy way to see what these various tests are is to look the test manpage.

In Unix, the /bin/[ command is actually a hard link to the /bin/test command. In early Unix systems, you would write this:

if test -n $parameter
then
    echo "Parameter has a value"
fi

or

if test $foo = $bar
then
    echo "Foo and Bar are equal"
fi

The /bin/[ was created, so you could do this:

if [ -n $parameter ]
then
    echo "Parameter has a value"
fi

and this

if [ $foo = $bar ]
then
    echo "Foo and Bar are equal"
fi

This explains why the funny syntax and why you need a space between the square brackets and the parameters inside.

The [[ ... ]] is actually a Korn shellism ... I mean a POSIX shellism that BASH has taken borrowed. It was introduced to allow pattern matching tests ([[ $foo == bar* ]]) and is internal to the shell, so its less sensitive to shell command line expansion issues. For example:

if [ $foo = $bar ]

will fail if either $foo or $bar is not set while:

if [[ $foo = $bar ]]

will work even if one of those two variables aren't set.

The [[ ... ]] syntax takes all of the same testing parameters that [ ... ] does and is now preferred.

2 Comments

But nobody uses [ ... ] without quotes! so [ "$foo" = "$bar" ] will work even if one is not set... besides, it will work even if foo or bar contain spaces.
@gniourf_gniourf Everybody should use quotes, or [ x$foo = x$bar ] (the last way prevented issues in Bourne shell when $foo started with a dash). However, people forget to use quotes, and the script works with the type of rough testing most people do with shell scripts before they're on a production system. It will work 99% of the time. That 1% of the time it will fail will happen at 2am during the time the system is most busy.
5

If $VARIABLE is a string, then [ -n $VARIABLE ] is true if the length of $VARIABLE is non-zero.

Also, [ -n $VARIABLE ] is equivalent with: [ $VARIABLE ], when and only when $VARIABLE is a string.

More about: Introduction to if

4 Comments

The two are not equivalent (i.e. VARIABLE=0).
@cforbish That's not the case. I start my answer saying that "If $VARAIBLE is a string".
You started a new sentence and it is not clear in the new sentence you mean only if the variable is a string. Consider an edit.
@cforbish no, [ 0 ] is true. -n $var is identical to $var, even the man page says as much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.