What is the If [[-n variable]] syntax used for bash

I commit old bash scripts that I often see

if [[ -n $VARIABLE ]]; then 
Syntax

I tried to do this, but could find why "-n" is used, the next thing 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

will anyone let me know what to do?

+4
source share
3 answers

help test will tell you:

String operators:

  ....

  -n STRING
     STRING      True if string is not empty.
+9
source

If it $VARIABLEis a string, then [ -n $VARIABLE ]true if the length is $VARIABLEnot equal to zero.

Also [ -n $VARIABLE ]equivalent: [ $VARIABLE ]when and only when $VARIABLEis a string.

Read more about: Introduction to

+3
source

, [[ ... ]] [ ... ] if while, Unix test. , , - test manpage.

Unix /bin/[ /bin/test. Unix :

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

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

/bin/[, :

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

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

, .

[[ ... ]] Korn shellism... POSIX-shellism, BASH . , ([[ $foo == bar* ]]) , . :

if [ $foo = $bar ]

, $foo, $bar , :

if [[ $foo = $bar ]]

, .

The syntax [[ ... ]]accepts all the same test parameters as [ ... ], and now it is preferred.

0
source

All Articles