SlackwareThis Forum is for the discussion of Slackware Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
$ /bin/[ --help
Usage: test EXPRESSION
or: test
or: [ EXPRESSION ]
or: [ ]
or: [ OPTION
Exit with the status determined by EXPRESSION.
--help display this help and exit
--version output version information and exit
An omitted EXPRESSION defaults to false. Otherwise,
EXPRESSION is true or false and sets exit status. It is one of:
( EXPRESSION ) EXPRESSION is true
! EXPRESSION EXPRESSION is false
EXPRESSION1 -a EXPRESSION2 both EXPRESSION1 and EXPRESSION2 are true
EXPRESSION1 -o EXPRESSION2 either EXPRESSION1 or EXPRESSION2 is true
-n STRING the length of STRING is nonzero
STRING equivalent to -n STRING
-z STRING the length of STRING is zero
STRING1 = STRING2 the strings are equal
STRING1 != STRING2 the strings are not equal
INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2
INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER2
INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2
INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER2
INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2
INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2
FILE1 -ef FILE2 FILE1 and FILE2 have the same device and inode numbers
FILE1 -nt FILE2 FILE1 is newer (modification date) than FILE2
FILE1 -ot FILE2 FILE1 is older than FILE2
-b FILE FILE exists and is block special
-c FILE FILE exists and is character special
-d FILE FILE exists and is a directory
-e FILE FILE exists
-f FILE FILE exists and is a regular file
-g FILE FILE exists and is set-group-ID
-G FILE FILE exists and is owned by the effective group ID
-h FILE FILE exists and is a symbolic link (same as -L)
-k FILE FILE exists and has its sticky bit set
-L FILE FILE exists and is a symbolic link (same as -h)
-O FILE FILE exists and is owned by the effective user ID
-p FILE FILE exists and is a named pipe
-r FILE FILE exists and read permission is granted
-s FILE FILE exists and has a size greater than zero
-S FILE FILE exists and is a socket
-t FD file descriptor FD is opened on a terminal
-u FILE FILE exists and its set-user-ID bit is set
-w FILE FILE exists and write permission is granted
-x FILE FILE exists and execute (or search) permission is granted
Except for -h and -L, all FILE-related tests dereference symbolic links.
Beware that parentheses need to be escaped (e.g., by backslashes) for shells.
INTEGER may also be -l STRING, which evaluates to the length of STRING.
NOTE: [ honors the --help and --version options, but test does not.
test treats each of those as it treats any other nonempty STRING.
NOTE: your shell may have its own version of test and/or [, which usually supersedes
the version described here. Please refer to your shell's documentation
for details about the options it supports.
Report [ bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils '[ invocation'
note in particular the part after the Synopses in the following extract from the info file
Code:
File: coreutils.info, Node: test invocation, Next: expr invocation, Prev: true invocation, Up: Conditions
16.3 `test': Check file types and compare values
================================================
`test' returns a status of 0 (true) or 1 (false) depending on the
evaluation of the conditional expression EXPR. Each part of the
expression must be a separate argument.
`test' has file status checks, string operators, and numeric
comparison operators.
`test' has an alternate form that uses opening and closing square
brackets instead a leading `test'. For example, instead of `test -d
/', you can write `[ -d / ]'. The square brackets must be separate
arguments; for example, `[-d /]' does not have the desired effect.
Since `test EXPR' and `[ EXPR ]' have the same meaning, only the former
form is discussed below.
Synopses:
test EXPRESSION
test
[ EXPRESSION ]
[ ]
[ OPTION
Due to shell aliases and built-in `test' functions, using an
unadorned `test' interactively or in a script may get you different
functionality than that described here. Invoke it via `env' (i.e.,
`env test ...') to avoid interference from the shell.
If EXPRESSION is omitted, `test' returns false. If EXPRESSION is a
single argument, `test' returns false if the argument is null and true
otherwise. The argument can be any string, including strings like
`-d', `-1', `--', `--help', and `--version' that most other programs
would treat as options. To get help and version information, invoke
the commands `[ --help' and `[ --version', without the usual closing
brackets. *Note Common options::.
Exit status:
0 if the expression is true,
1 if the expression is false,
2 if an error occurred.
* Menu:
* File type tests:: -[bcdfhLpSt]
* Access permission tests:: -[gkruwxOG]
* File characteristic tests:: -e -s -nt -ot -ef
* String tests:: -z -n = == !=
* Numeric tests:: -eq -ne -lt -le -gt -ge
* Connectives for test:: ! -a -o
No, /bin/[ is not a shell builtin. bash does have both '[' and '[[' as builtins -and they are the ones that usually get used in scripts, unless /bin/[ gets called explicitly with the full path. The same is true for 'test' and /bin/test. And, /bin/[ is not simply a copy of /bin/test -although some systems might have /bin/[ as a link to /bin/test.
If you run '/bin/[ --help' you will not get the same results as '/bin/test' or 'help [' (from the bash command line).
So, in short, '[' & 'test' (from shell) and '/bin/[' & '/bin/test' are four separate things -each with slightly different features. And, of course, the double '[[' is only available as a shell builtin. One other helpful thing is to use the shell built-in 'type' instead of 'which' in order to identify which binary is really being used.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.