What do the -n and -a options do in a bash if statement? [duplicate]

-a Links two expressions together in an “and” or “&&” expression. This option is deprecated.

-n Checks if the length of a string is nonzero.

You could translate the test expression into the following pseudocode:

if ( ($1 has nonzero length) and
     ($2 has nonzero length) and
     ($3 has nonzero length) )

There are no checks in that expression for whether the file exists or doesn’t exist, only whether the arguments have been supplied to the script.

The arguments -a and -n can be found in the manpage for test

man test

The operator [ ... ] is often used as shorthand for test ... and likely has identical functionality on your system.

Leave a Comment