Session and utilities

The test command

Evaluates a condition and returns success or failure. Used in scripts and with [ ].

Syntax

test <condicion>Try it in the terminal →

Most useful options

-e archivoThe file exists, whatever its type.
-f archivoIt exists and is a regular file, not a folder.
-d carpetaIt exists and is a directory.
-s archivoIt exists and its size is greater than zero.
-r, -w, -x archivoYou have read, write or execute permission on it.
-z, -n cadena-z: the string is empty. -n: it is not empty.
=, != cadena1 cadena2The two strings are equal or different. It compares text, not numbers.
-eq, -ne, -lt, -le, -gt, -ge n1 n2Compares two integers: equal, not equal, less, less or equal, greater, greater or equal.

Copy-ready examples

$ test -d Documentos && echo sichecks whether the Documentos folder exists and answers.

Example output

si
$ test -s lleno.txt; echo $?a file with content returns 0, meaning success.

Example output

0
$ test -s vacio.txt; echo $?an empty file returns 1, meaning failure.

Example output

1
$ test 5 -gt 3; echo $?compares two integers: 5 is greater than 3.

Example output

0
$ [ -n "$HOME" ] && echo "hay carpeta personal"uses [ ] in a script: it prints only if the HOME variable has a value.

Example output

hay carpeta personal

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
bash: test: abc: integer expression expected-eq only accepts integers and received the text abc, for example because a variable is empty or contains letters.Check that the variable holds a number before comparing, or use = if you want to compare text.
bash: [: missing `]'You wrote [ without the closing ], or without the space before it.Close the expression with ] separated by a space.
bash: [: =: unary operator expectedAn unquoted variable became empty and the comparison lost an operand.Put the variable in quotes: [ "$v" = x ].

Typical mistakes

  • test prints nothing: it only changes the exit status ($?). To see it, use echo $? right after.
  • Without quotes, an empty variable disappears and the comparison goes wrong: write [ "$v" = x ].
  • -a and -o are ambiguous with many arguments: chain two test commands with && or ||.

Differences from similar commands

  • expr: computes and prints a result. test prints nothing: it only returns success or failure.
  • [[: a bash keyword. Inside it words are not split, and it accepts && and || without leaving the expression.

Related commands

FAQ

Why does [ need the ] at the end?

Because [ is a normal command that receives its arguments, and the ] is the last argument marking where the expression ends.

How do I check that one number is greater than another?

With -gt: [ "$a" -gt "$b" ] is true if a is greater than b. Use -ge for greater or equal.

Why does my condition fail when the file does exist?

Almost always because of the path: a relative path is looked up from the current folder, not from where the script is. Use an absolute path or pwd to check.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course