Faster Learning Tutorials

Linux test command summary with examples

test allows you to evaluate an expression.
  1. Purpose - Learn what test is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with test.
  4. Script - Add test to our script and run it.
  5. A tip - Finish off with one more insight.
Paul Alan Davis, CFA, November 17, 2016
Updated: August 6, 2018
In this tutorial, 93 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Next

~/ home  / tech  / linux essentials  / test command

The Linux test command


Learn to evaluate an expression in a shell script at the Linux command line.

Video Tutorial

Linux test command summary with examples (3:58)

Videos can also be accessed from the YouTube Playlist.

Video Script

The Command and Why You Need It

Our ninety-third word, or command to memorize is test from our category Workflow.

test allows you to evaluate an expression.

Recall from videos (tutorials) #87 to #92, we've been building a script to demonstrate workflow, here we'll use test at the command line, and return to programming a script in our next few videos (tutorials).

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The test command has 2 options and the argument is an expression, which is something to evaluate, like is 10 greater than 9. Also note, square brackets [ ] around the expression really does the same thing.

Like most commands, help is available with double-dash --help, that is, if you're using the test program that is not built-in to your shell (otherwise see man bash). Okay, test evaluates strings, integers and files and gives an exit status of 0 for true and other integers if it's false, giving us program logic.

So why is test an important command? Well, you need a way to interpret these conditions. And now you know how to do that.


Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. As with other built-ins, type -a tells us whether we have multiple versions, which can impact how you find help, for example.

$ type -a test test is a shell builtin test is /usr/bin/test

As mine did.

And then, a test of integers. Let's start with that in a test 10 = 10

$ test 10 = 10 $ _

And it looks like nothing happened. Well actually, it did, but we need to go get the exit status, like this with echo $?.

$ echo $? 0

And zero is for success (true), as I mentioned.

Or, we could do this in brackets (a synomyn). Is [ joe = joe ].

$ [ joe = joe ]; echo $? 0

And exit status here says true. But test joe blo = joe blo.

$ test joe blo = joe blo; echo $? -bash: test: too many arguments 2

And bash will yell at you if you don't surround it in quotes. We know about that one (because it then becomes four items to test instead of two).

And then on files, using options that say test -e, for does it exist? Then the file name, let's say we go look for our .bashrc file, and the exit status here is true.

$ test -e .bashrc; echo $? 0

And last, just for curiousity, let's see how test interprets nothing.

$ test; echo $? 1

False, hmm, interesting.

A Final Tip

Okay now you know how to use test. And you know the syntax for commands, options and arguments.

One last tip about the test command. So it's likely you'll see test used more often with brackets [ ], but really it is up to you.

Okay, thanks for visiting today. I hope this was a helpful introduction to the test command.

Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:

What's Next?

You don't want to miss new videos on our YouTube Channel. Subscribe here.

  • For the Outline to all 100 tutorials, click Outline.
  • To go back to the printf command, hit Back.
  • To see the very important if keyword used to build conditional logic, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / test command

linux test command
bash if then else
bash script
shell if
bash test
test Linux
bash test operator
unix test
shell test
if condition in shell script
bash test command
test command syntax
bash string comparison
bash file exists
shell script test
bash condition
man test
test command examples