What is this file: /usr/bin/[?

The name of the file is '[' . When I open it up in vi it looks like it's a copy of 'test'. It's owned by root.


Solution 1:

That file is supposed to be there (or under /usr/bin/[). It allows you to type, for instance, [ -f $HOME/.bashrc ] && echo ".bashrc exists", which is equivalent to test -f $HOME/.bashrc && echo ".bashrc exists".

Reality is a bit more complicated, since bash overrides /usr/bin/[ with its own [, so you can use the [ executable by providing a full path, /usr/bin/[ -f $HOME/.bashrc ] && echo ".bashrc exists".

Solution 2:

It is a version of test that requires a ] as last argument. It's often used in shell scripts, like in

if [ -e some_file ]; then ...

(but in most modern shell, like Bash, it's a builtin command).

It's required by POSIX