Is there a list of 'if' switches anywhere?

BashIf StatementSwitch StatementShell Exec

Bash Problem Overview


Is there a list of all the if switches for use in Bash scripting? Sometimes I see someone using it and I wonder what the switch they're using actually does.

An example is the -z in this one. I know how to use it, but I don't know where it was derived from.

if [ -z "$BASH_VERSION" ]; then
    echo -e "Error: this script requires the BASH shell!"
    exit 1
fi

Any references, guides, posts, answers would be appreciated.

Bash Solutions


Solution 1 - Bash

Look at the Bash man page (man bash). The options are specified in the CONDITIONAL EXPRESSIONS section:

CONDITIONAL EXPRESSIONS
       Conditional expressions are used by the [[  compound  command  and  the
       test  and [ builtin commands to test file attributes and perform string
       and arithmetic comparisons.  Expressions are formed from the  following
       unary  or  binary  primaries.   If any file argument to one of the pri-
       maries is of the form /dev/fd/n, then file descriptor n is checked.  If
       the  file  argument  to  one  of  the  primaries  is one of /dev/stdin,
       /dev/stdout, or /dev/stderr, file descriptor 0, 1, or 2,  respectively,
       is checked.

       Unless otherwise specified, primaries that operate on files follow sym-
       bolic links and operate on the target of the link, rather than the link
       itself.

       -a file
              True if file exists.
       ... more options ...

It is also explained in the help:

$ help [
[: [ arg... ]
    This is a synonym for the "test" builtin, but the last
    argument must be a literal `]', to match the opening `['.

Solution 2 - Bash

Yes. These are called conditional expressions and these are used by the [[ compound command and the test and [ builtin commands ([ is simply a synonym for test).

Read section 6.4 Bash Conditional Expressions of the Bash Reference Manual, which contains a list of all these switches and their usage.

Solution 3 - Bash

They are not switches for the if statement, but for the test command ([ is a synonym for the test builtin). See help test in Bash for a complete list.

Solution 4 - Bash

The single square brackets ([ ... ]) is an synonym of the test command. If you look at the man page for test, you will see almost all (Bash might have a few extra not mentioned here) of the various if switches as you called them. All in one easy-to-find place.

If you use double square brackets ([[ ... ]]), you are using an extended Bash set of tests. These mainly have to do with regular expression matching, and glob matching (and extended glob matching if you have that set too). For that, you'll have to read that Bash man page.

You called them if switches, but that's not really correct. These are tests and really have nothing to do with the if command.

The if command merely executes the command you give it, and then if that command returns an exit code of 0, will run the if portion of the if statement. Otherwise, it will run the else portion (if that's present).

Let's look at this:

rm foo.test.txt   # Hope this wasn't an important file
if ls foo.test.txt

> then
> echo "This file exists"
> else
> echo "I can't find it anywhere.."
> fi
ls: foo.test.txt: No such file or directory
I can't find it anywhere..

The if statement runs the ls foo.test.txt command and the ls command returns a non-zero because the file does not exist. This causes the if statement to execute the else clause.

Let's try that again...

touch foo.test.txt   # Now this file exists.
if ls foo.test.txt   # Same "if/else" statement as above

> then
> echo "This file exists"
> else
> echo "I can't find it anywhere.."
> fi
foo.test.txt
This file exists

Here, the ls command returned a 0 exit status (since the file exists and the file exists and can be stat'ed by the ls command.

Normally, you shouldn't use the ls command to test for a file. I merely used it here to show that the if statement executes the command, then executed the if or else clause depending upon the exit status of that command. If you want to test whether or not a file exists, you should use the test -e command instead of ls command:

if test -e foo.test.txt  # The same as above, but using "test" instead of "ls"
then
    echo "This file exists"
else
    echo "I can't find it anywhere..."
fi

If the file exists, test -e will return an exit status of 0. Otherwise, it will return a non-zero exit status.

If you do this:

ls -i /bin/test /bin/[

10958 /bin/[    10958 /bin/test

That 10958 is the inode. Files with the same inode are two different names for the same file. Thus [ and test command are soft linked1. This means you can use [ instead of test:

if [ -e foo.test.txt ]
then
    echo "This file exists"
else
    echo "I can't find it anywhere.."
fi

Does it look familiar?


1. In Bash, the test and [ are builtin, so when you run these commands in BASH, it isn't running /bin/test or /bin/[. However, they're still linked to each other.

Solution 5 - Bash

It's actually not if that's providing those — it's [, better known by the name of test. help test should give you a list of all options it can take. You could also look at the standard, if you care.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanijel-James WView Question on Stackoverflow
Solution 1 - BashSheetJSView Answer on Stackoverflow
Solution 2 - BashJohnsywebView Answer on Stackoverflow
Solution 3 - BashDolda2000View Answer on Stackoverflow
Solution 4 - BashDavid W.View Answer on Stackoverflow
Solution 5 - BashicktoofayView Answer on Stackoverflow