ArticleslgStudy

science

Test (Unix)

Test (Unix) is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Test (Unix) rather than just read about it. In short: test is a command-line utility found in Unix, Plan 9, and Unix-like operating systems that evaluates conditional expressions. test was turned into a shell builtin command in 1981 with UNIX System III and at the same time made available under the alternate name [. Overview The test command in Unix evaluates the expression parameter.

Key takeaways

  • Test (Unix) belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Test (Unix) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Test (Unix) from memory before moving on to harder problems.

Reference excerpt

test is a command-line utility found in Unix, Plan 9, and Unix-like operating systems that evaluates conditional expressions. test was turned into a shell builtin command in 1981 with UNIX System III and at the same time made available under the alternate name [.

Overview The test command in Unix evaluates the expression parameter. In most recent shell implementations, it is a shell builtin, even though the external version still exists. In the second form of the command, the [ ] (brackets) must be surrounded by blank spaces (this is because [ is a program and POSIX compatible shells require a space between the program name and its arguments). One must test explicitly for file names in the C shell. File-name substitution (globbing) causes the shell script to exit. The test command is not to be confused with the [[ reserved word that was introduced with ksh88. The latter is not a command but part of the ksh88 syntax and does not apply file-name substitution to glob expressions. The version of test bundled in GNU coreutils was written by Kevin Braunsdorf and Matthew Bradburn. The command is available as a separate package for Microsoft Windows as part of the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities. The test command has also been ported to the IBM i operating system.

Syntax test expression or [ expression ]

Arguments The following arguments are used to construct this parameter. All arguments return True if the object (file or string) exists, and the condition specified is true.

For the -x argument, if the specified file exists and is a directory, the True exit value indicates that the current process has permission to change cd into the directory.

Non standard Korn Shell extensions file1 -nt file2 - file1 is newer than file2 file1 -ot file2 - file1 is older than file2 file1 -ef file2 - file1 is another name for file2 - (symbolic link or hard link)

String arguments In Perl, these sections are reversed: eq is a string operator and == is a numerical operator, and so on for the others.

-n String1 - the length of the String1 variable is nonzero -z String1 - the length of the String1 variable is 0 (zero) String1 = String2 - String1 and String2 variables are identical String1 != String2 - String1 and String2 variables are not identical String1 - true if String1 variable is not a null string

Number arguments Integer1 -eq Integer2 - Integer1 and Integer2 variables are algebraically equal -ne - not equal -gt - greater than -ge - greater or equal -lt - less than -le - less or equal

Operators test arguments can be combined with the following operators:

! - Unary negation operator -a - Binary AND operator -o - Binary OR operator (the -a operator has higher precedence than the -o operator) \(Expression\) - Parentheses for grouping must be escaped with a backslash \

The -a and -o operators, along with parentheses for grouping, are XSI extensions and are therefore not portable. In portable shell scripts, the same effect may be achieved by connecting multiple invocations of test together with the && and || operators and parentheses.

Exit status This command returns the following exit values:

0 - The Expression parameter is true 1 - The Expression parameter is false or missing >1 - An error occurred

Examples 1. To test whether a file is nonexistent or empty, type:

If the file specified by the first positional parameter to the shell procedure, $1, does not exist or is of size 0, the test command displays the message. If $1 exists and has a size greater than 0, the test command displays nothing. Note: There must be a space between the -s function and the file name. The quotation marks around $1 ensure that the test works properly even if the value of $1 is a null string. If the quotation marks are omitted and $1 is the empty string, the test command displays the error message:

test: argument expected.

2. To do a complex comparison, type:

If the shell procedure is given fewer than two positional parameters or the file specified by $1 does not exist, then the shell procedure exits. The special shell variable $# represents the number of positional parameters entered on the command line that starts this shell procedure.

See also List of POSIX commands Unix shell find (Unix)

References

Further reading Robbins, Arnold; Nelson H. F. Beebe (2005). "6.2.4 The test Command". Classic Shell Scripting: Hidden Commands that Unlock the Power of Unix. O'Reilly Media, Inc. pp. 120–128. ISBN 978-0-596-55526-9. Ian Shields (20 February 2007). "Linux tip: Bash test and comparison functions". IBM DeveloperWorks. William Shotts (2013). "27 – Flow Control: Branching With if". The Linux Command Line. No Starch Press. pp. 381–390. ISBN 978-1-59327-389-7. (free download)

External links

test – Shell and Utilities Reference, The Single UNIX Specification, Version 5 from The Open Group test(1) – Linux User Commands Manual test(1) – Plan 9 Programmer's Manual, Volume 1

Worked examples

Example 1 — a first encounter with Test (Unix)

Start with the simplest possible case. Write down what Test (Unix) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Test (Unix) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Test (Unix) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Test (Unix)

In research
Test (Unix) appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Test (Unix) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Test (Unix) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Conditional constructs, IBM i Qshell commands, Plan 9 commands, so understanding it makes those chapters shorter.
In everyday life
Look for Test (Unix) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Test (Unix)” →

How to study Test (Unix) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Test (Unix) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Test (Unix) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Test (Unix) in simple terms?

test is a command-line utility found in Unix, Plan 9, and Unix-like operating systems that evaluates conditional expressions. test was turned into a shell builtin command in 1981 with UNIX System III and at the same time made available under the alternate name [. Overview The test command in Unix e…

Why does Test (Unix) matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Test (Unix)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Test (Unix).

Tags

  • Conditional constructs
  • IBM i Qshell commands
  • Plan 9 commands
  • Unix SUS2008 utilities

Keep exploring