Test (Unix)

Last updated
test
Other names[
Developer(s) Various open-source and commercial developers
Written in C
Operating system Unix, Unix-like, Plan 9, IBM i
Platform Cross-platform
Type Command
License coreutils: GPLv3+
Plan 9: MIT License

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 [. [1]

Contents

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. [2] 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. [3] The test command has also been ported to the IBM i operating system. [4]

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.

ArgumentReturns True if the file
-bis a block special file
-cis a character special file
-dis a directory
-eexists
-fis a regular file
-ghas the Set Group ID bit set
-his a symbolic link
-khas the sticky bit set
-Lis a symbolic link
-pis a named pipe (FIFO)
-ris readable by the current process
-shas a size greater than 0
-t FileDescriptor is open and associated with a terminal
-uhas the Set User ID bit set
-whas the write flag is on
-xhas execute flag on

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 [5] 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:

iftest!-s"$1"thenecho$1doesnotexistorisempty. fi

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["$#"-lt2]||![-e"$1"]thenexitfi

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

Related Research Articles

In Unix and Unix-like operating systems, chmod is the command and system call used to change the access permissions and the special mode flags of file system objects. Collectively these were originally called its modes, and the name chmod was chosen as an abbreviation of change mode.

<span class="mw-page-title-main">C shell</span> Unix shell

The C shell is a Unix shell created by Bill Joy while he was a graduate student at University of California, Berkeley in the late 1970s. It has been widely distributed, beginning with the 2BSD release of the Berkeley Software Distribution (BSD) which Joy first distributed in 1978. Other early contributors to the ideas or the code were Michael Ubell, Eric Allman, Mike O'Brien and Jim Kulp.

uname Standard UNIX utility that prints name and other details about the machine

uname is a computer program in Unix and Unix-like computer operating systems that prints the name, version and other details about the current machine and the operating system running on it.

ln (Unix) Unix file management utility

The ln command is a standard Unix command utility used to create a hard link or a symbolic link (symlink) to an existing file or directory. The use of a hard link allows multiple filenames to be associated with the same file since a hard link points to the inode of a given file, the data of which is stored on disk. On the other hand, symbolic links are special files that refer to other files by name.

In computing, touch is a command used to update the access date and/or modification date of a computer file or directory. It is included in Unix and Unix-like operating systems, TSC's FLEX, Digital Research/Novell DR DOS, the AROS shell, the Microware OS-9 shell, and ReactOS. The command is also available for FreeDOS and Microsoft Windows.

cp (Unix) Unix command utility

In computing, cp is a command in various Unix and Unix-like operating systems for copying files and directories. The command has three principal modes of operation, expressed by the types of arguments presented to the program for copying a file to another file, one or more files to a directory, or for copying entire directories to another directory.

paste is a Unix command line utility which is used to join files horizontally by outputting lines consisting of the sequentially corresponding lines of each file specified, separated by tabs, to the standard output.

The GNU Core Utilities or coreutils is a package of GNU software containing implementations for many of the basic tools, such as cat, ls, and rm, which are used on Unix-like operating systems.

The Thompson shell was the first Unix shell, introduced in the first version of Unix in 1971, and was written by Ken Thompson. It was a simple command interpreter, not designed for scripting, but nonetheless introduced several innovative features to the command-line interface and led to the development of the later Unix shells.

Expect is an extension to the Tcl scripting language written by Don Libes. The program automates interactions with programs that expose a text terminal interface. Expect, originally written in 1990 for the Unix platform, has since become available for Microsoft Windows and other systems.

rm (Unix) Unix command utility

rm is a basic command on Unix and Unix-like operating systems used to remove objects such as computer files, directories and symbolic links from file systems and also special files such as device nodes, pipes and sockets, similar to the del command in MS-DOS, OS/2, and Microsoft Windows. The command is also available in the EFI shell.

<span class="mw-page-title-main">Comparison of command shells</span>

A command shell is a command-line interface to interact with and manipulate a computer's operating system.

In computing, tee is a command in command-line interpreters (shells) using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. It is primarily used in conjunction with pipes and filters. The command is named after the T-splitter used in plumbing.

In computing, sleep is a command in Unix, Unix-like and other operating systems that suspends program execution for a specified time.

expr is a command line utility on Unix and Unix-like operating systems which evaluates an expression and outputs the corresponding value. It first appeared in Unix v7. The command is available for Microsoft Windows as part of the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities. The expr command has also been ported to the IBM i operating system.

In computing, a shebang is the character sequence consisting of the characters number sign and exclamation mark at the beginning of a script. It is also called sharp-exclamation, sha-bang, hashbang, pound-bang, or hash-pling.

In a Unix shell, the full stop called the dot command (.) is a command that evaluates commands in a computer file in the current execution context. In the C shell, a similar functionality is provided as the source command, and this name is seen in "extended" POSIX shells as well.

printf (Unix) Standard UNIX utility

In Unix and Unix-like operating systems, printf is a shell builtin that formats and prints data.

The csplit command in Unix and Unix-like operating systems is a utility that is used to split a file into two or more smaller files determined by context lines.

In computing, process substitution is a form of inter-process communication that allows the input or output of a command to appear as a file. The command is substituted in-line, where a file name would normally occur, by the command shell. This allows programs that normally only accept files to directly read from or write to another program.

References

  1. http://www.in-ulm.de/~mascheck/bourne/#system3 Bourne Shell changes with System III
  2. test(1) — coreutils — Debian buster — Debian Manpages
  3. "Native Win32 ports of some GNU utilities". unxutils.sourceforge.net.
  4. IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). IBM . Retrieved 2020-09-05.
  5. IEEE Std 1003.1, 2004, documentation for test

Further reading