FactorPad
Build a Better Process

Linux test Command Summary with Examples

Linux 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.
face pic by Paul Alan Davis, CFA
Updated: February 24, 2021
In this tutorial on Linux test, 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 Tip Next

/ factorpad.com / tech / linux-essentials / test-command.html


An ad-free and cookie-free website.


Examples of the Linux test Command

Intermediate

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

Video Tutorial

Videos can also be accessed from the YouTube Linux Essentials Playlist on YouTube.

Linux test Command Summary with Examples (3:58)

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.

Common Linux test Options
-options description
--help Print help screen
--version Show version information

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.

Demonstration

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 content, so subscribe, follow and join.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / test-command.html


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

A newly-updated free resource. Connect and refer a friend today.