/ factorpad.com / tech / linux-essentials / test-command.html
An ad-free and cookie-free website.
Intermediate
Learn to evaluate an expression in a shell script at the Linux command line.
Videos can also be accessed from the YouTube Linux Essentials Playlist on YouTube.
Linux test Command Summary with Examples (3:58)
Our ninety-third word, or command to memorize is
test
from our category
Workflow.
test
allows you to evaluate an
expression.
-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.
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.
As mine did.
And then, a test of integers. Let's start with that in a
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 $?
.
And zero is for success (true), as I mentioned.
Or, we could do this in brackets (a synomyn). Is
[ joe = joe ]
.
And exit status here says true. But
test joe blo = joe blo
.
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.
And last, just for curiousity, let's see how
test
interprets nothing.
False, hmm, interesting.
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.
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:
You don't want to miss new content, so subscribe, follow and join.
printf
command,
hit Back.if
keyword used to build conditional logic, click Next./ factorpad.com / tech / linux-essentials / test-command.html
A newly-updated free resource. Connect and refer a friend today.