2019-03-31 17:05:09 +08:00
.. _cmd-test:
2018-12-17 09:39:33 +08:00
test - perform tests on files and text
2019-01-03 12:10:47 +08:00
======================================
2018-12-17 09:39:33 +08:00
2018-12-18 09:58:24 +08:00
Synopsis
--------
2018-12-17 05:08:41 +08:00
docs synopsis: add HTML highlighing and automate manpage markup
Recent synopsis changes move from literal code blocks to
[RST line blocks]. This does not translate well to HTML: it's not
rendered in monospace, so aligment is lost. Additionally, we don't
get syntax highlighting in HTML, which adds differences to our code
samples which are highlighted.
We hard-wrap synopsis lines (like code blocks). To align continuation
lines in manpages we need [backslashes in weird places]. Combined with
the **, *, and `` markup, it's a bit hard to get the alignment right.
Fix these by moving synopsis sources back to code blocks and compute
HTML syntax highlighting and manpage markup with a custom Sphinx
extension.
The new Pygments lexer can tokenize a synopsis and assign the various
highlighting roles, which closely matches fish's syntax highlighing:
- command/keyword (dark blue)
- parameter (light blue)
- operator like and/or/not/&&/|| (cyan)
- grammar metacharacter (black)
For manpage output, we don't project the fish syntax highlighting
but follow the markup convention in GNU's man(1):
bold text type exactly as shown.
italic text replace with appropriate argument.
To make it easy to separate these two automatically, formalize that
(italic) placeholders must be uppercase; while all lowercase text is
interpreted literally (so rendered bold).
This makes manpages more consistent, see string-join(1) and and(1).
Implementation notes:
Since we want manpage formatting but Sphinx's Pygments highlighing
plugin does not support manpage output, add our custom "synopsis"
directive. This directive parses differently when manpage output is
specified. This means that the HTML and manpage build processes must
not share a cache, because the parsed doctrees are cached. Work around
this by using separate cache locations for build targets "sphinx-docs"
(which creates HTML) and "sphinx-manpages". A better solution would
be to only override Sphinx's ManualPageBuilder but that would take a
bit more code (ideally we could override ManualPageWriter but Sphinx
4.3.2 doesn't really support that).
---
Alternative solution: stick with line blocks but use roles like
:command: or :option: (or custom ones). While this would make it
possible to produce HTML that is consistent with code blocks (by adding
a bit of CSS), the source would look uglier and is harder to maintain.
(Let's say we want to add custom formatting to the [|] metacharacters
in HTML. This is much easier with the proposed patch.)
---
[RST line blocks]: https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#line-blocks
[backslashes in weird places]: https://github.com/fish-shell/fish-shell/pull/8626#discussion_r782837750
2022-01-09 22:09:46 +08:00
.. synopsis ::
test [EXPRESSION]
[ [EXPRESSION] ]
2018-12-18 09:58:24 +08:00
2018-12-19 10:44:30 +08:00
Description
2019-01-03 12:10:47 +08:00
-----------
2018-12-17 05:08:41 +08:00
2021-07-17 00:21:41 +08:00
.. only :: builder_man
NOTE: This page documents the fish builtin `` test `` .
To see the documentation on the `` test `` command you might have,
use `` command man test `` .
2023-03-01 03:43:36 +08:00
`` test `` checks the given conditions and sets the exit status to 0 if they are true, 1 if they are false.
2018-12-17 05:08:41 +08:00
2022-01-16 19:09:51 +08:00
The first form (`` test `` ) is preferred. For compatibility with other shells, the second form is available: a matching pair of square brackets (`` [ [EXPRESSION] ] `` ).
2018-12-17 05:08:41 +08:00
2023-07-07 00:39:42 +08:00
When using a variable or command substitution as an argument with `` test `` you should almost always enclose it in double-quotes, as variables expanding to zero or more than one argument will most likely interact badly with `` test `` .
For historical reasons, `` test `` supports the one-argument form (`` test foo `` ), and this will also be triggered by e.g. `` test -n $foo `` if $foo is unset. We recommend you don't use the one-argument form and quote all variables or command substitutions used with `` test `` .
2018-12-17 05:08:41 +08:00
2018-12-19 10:44:30 +08:00
Operators for files and directories
2019-01-03 12:10:47 +08:00
-----------------------------------
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-b** *FILE*
Returns true if *FILE* is a block device.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-c** *FILE*
Returns true if *FILE* is a character device.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-d** *FILE*
Returns true if *FILE* is a directory.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-e** *FILE*
Returns true if *FILE* exists.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-f** *FILE*
Returns true if *FILE* is a regular file.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-g** *FILE*
Returns true if *FILE* has the set-group-ID bit set.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-G** *FILE*
Returns true if *FILE* exists and has the same group ID as the current user.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-k** *FILE*
Returns true if *FILE* has the sticky bit set. If the OS does not support the concept it returns false. See https://en.wikipedia.org/wiki/Sticky_bit.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-L** *FILE*
Returns true if *FILE* is a symbolic link.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-O** *FILE*
Returns true if *FILE* exists and is owned by the current user.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-p** *FILE*
Returns true if *FILE* is a named pipe.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-r** *FILE*
Returns true if *FILE* is marked as readable.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-s** *FILE*
Returns true if the size of *FILE* is greater than zero.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-S** *FILE*
Returns true if *FILE* is a socket.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-t** *FD*
Returns true if the file descriptor *FD* is a terminal (TTY).
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-u** *FILE*
Returns true if *FILE* has the set-user-ID bit set.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-w** *FILE*
Returns true if *FILE* is marked as writable; note that this does not check if the filesystem is read-only.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-x** *FILE*
Returns true if *FILE* is marked as executable.
2018-12-17 05:08:41 +08:00
2022-07-08 20:21:46 +08:00
Operators to compare files and directories
------------------------------------------
*FILE1* **-nt** *FILE2*
Returns true if *FILE1* is newer than *FILE2* , or *FILE1* exists and *FILE2* does not.
*FILE1* **-ot** *FILE2*
Returns true if *FILE1* is older than *FILE2* , or *FILE2* exists and *FILE1* does not.
*FILE1* **-ef** *FILE1*
Returns true if *FILE1* and *FILE2* refer to the same file.
2018-12-19 10:44:30 +08:00
Operators for text strings
2019-01-03 12:10:47 +08:00
--------------------------
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
*STRING1* **=** *STRING2*
Returns true if the strings *STRING1* and *STRING2* are identical.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
*STRING1* **!=** *STRING2*
Returns true if the strings *STRING1* and *STRING2* are not identical.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-n** *STRING*
Returns true if the length of *STRING* is non-zero.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**-z** *STRING*
Returns true if the length of *STRING* is zero.
2018-12-17 05:08:41 +08:00
2018-12-19 10:44:30 +08:00
Operators to compare and examine numbers
2019-01-03 12:10:47 +08:00
----------------------------------------
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
*NUM1* **-eq** *NUM2*
Returns true if *NUM1* and *NUM2* are numerically equal.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
*NUM1* **-ne** *NUM2*
Returns true if *NUM1* and *NUM2* are not numerically equal.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
*NUM1* **-gt** *NUM2*
Returns true if *NUM1* is greater than *NUM2* .
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
*NUM1* **-ge** *NUM2*
Returns true if *NUM1* is greater than or equal to *NUM2* .
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
*NUM1* **-lt** *NUM2*
Returns true if *NUM1* is less than *NUM2* .
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
*NUM1* **-le** *NUM2*
Returns true if *NUM1* is less than or equal to *NUM2* .
2018-12-17 05:08:41 +08:00
Both integers and floating point numbers are supported.
2018-12-19 10:44:30 +08:00
Operators to combine expressions
2019-01-03 12:10:47 +08:00
--------------------------------
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
*COND1* **-a** *COND2*
Returns true if both *COND1* and *COND2* are true.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
*COND1* **-o** *COND2*
Returns true if either *COND1* or *COND2* are true.
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
Expressions can be inverted using the **!** operator:
2018-12-17 05:08:41 +08:00
2022-03-11 23:56:20 +08:00
**!** *EXPRESSION*
Returns true if *EXPRESSION* is false, and false if *EXPRESSION* is true.
2018-12-17 05:08:41 +08:00
Expressions can be grouped using parentheses.
2022-03-11 23:56:20 +08:00
**(** *EXPRESSION* **)**
Returns the value of *EXPRESSION* .
2018-12-17 05:08:41 +08:00
2023-08-21 04:10:30 +08:00
Note that parentheses will usually require escaping with `` \ `` (so they appear as `` \( `` and `` \) `` ) to avoid being interpreted as a command substitution.
2018-12-17 05:08:41 +08:00
2018-12-19 10:44:30 +08:00
Examples
2019-01-03 12:10:47 +08:00
--------
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
If the `` /tmp `` directory exists, copy the `` /etc/motd `` file to it:
2018-12-17 05:08:41 +08:00
2018-12-19 11:14:04 +08:00
::
if test -d /tmp
cp /etc/motd /tmp/motd
end
2018-12-17 05:08:41 +08:00
2022-03-12 00:18:41 +08:00
If the variable :envvar: `MANPATH` is defined and not empty, print the contents. (If :envvar: `MANPATH` is not defined, then it will expand to zero arguments, unless quoted.)
2018-12-17 05:08:41 +08:00
2018-12-19 11:14:04 +08:00
::
if test -n "$MANPATH"
echo $MANPATH
end
2023-07-07 00:39:42 +08:00
Be careful with unquoted variables::
if test -n $MANPATH
# This will also be reached if $MANPATH is unset,
# because in that case we have `test -n` , so it checks if "-n" is non-empty, and it is.
echo $MANPATH
end
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
Parentheses and the `` -o `` and `` -a `` operators can be combined to produce more complicated expressions. In this example, success is printed if there is a `` /foo `` or `` /bar `` file as well as a `` /baz `` or `` /bat `` file.
2018-12-17 05:08:41 +08:00
2018-12-19 11:14:04 +08:00
::
if test \( -f /foo -o -f /bar \) -a \( -f /baz -o -f /bat \)
echo Success.
2022-03-23 21:17:00 +08:00
end
2018-12-19 11:14:04 +08:00
2018-12-17 05:08:41 +08:00
Numerical comparisons will simply fail if one of the operands is not a number:
2018-12-19 11:14:04 +08:00
::
if test 42 -eq "The answer to life, the universe and everything"
echo So long and thanks for all the fish # will not be executed
end
2022-03-12 00:18:41 +08:00
A common comparison is with :envvar: `status` :
2018-12-17 05:08:41 +08:00
2018-12-19 11:14:04 +08:00
::
if test $status -eq 0
echo "Previous command succeeded"
end
2018-12-17 05:08:41 +08:00
The previous test can likewise be inverted:
2018-12-19 11:14:04 +08:00
::
if test ! $status -eq 0
echo "Previous command failed"
end
2018-12-17 05:08:41 +08:00
which is logically equivalent to the following:
2018-12-19 11:14:04 +08:00
::
if test $status -ne 0
echo "Previous command failed"
end
2018-12-17 05:08:41 +08:00
2018-12-19 10:44:30 +08:00
Standards
2019-01-03 12:10:47 +08:00
---------
2018-12-17 05:08:41 +08:00
2023-03-01 03:43:36 +08:00
Unlike many things in fish, `` test `` implements a subset of the `IEEE Std 1003.1-2008 (POSIX.1) standard <https://www.unix.com/man-page/posix/1p/test/> `__ . The following exceptions apply:
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
- The `` < `` and `` > `` operators for comparing strings are not implemented.
2018-12-17 05:08:41 +08:00
2018-12-20 04:02:45 +08:00
In cases such as this, one can use `` command `` `` test `` to explicitly use the system's standalone `` test `` rather than this `` builtin `` `` test `` .
2023-03-01 03:43:36 +08:00
See also
--------
Other commands that may be useful as a condition, and are often easier to use:
- :doc: `string` , which can do string operations including wildcard and regular expression matching
- :doc: `path` , which can do file checks and operations, including filters on multiple paths at once