What is the `[` command?

4

1

I have seen both in Cygwin and in Linux the [ command under /bin. Whenever I run it I get an error saying: missing ].

I never understood what this command does, I just one day found it wanted to find out what it does (and whether it is useful or not)

NOTE: I would have checked the man entry, for an answer, but sadly, there is no entry in it for [

inixsoftware

Posted 2014-03-14T23:10:24.913

Reputation: 330

Question was closed 2014-03-17T19:44:57.870

What is the command? – stderr – 2014-03-14T23:18:36.650

1@TiagoƇ. The command is called [ – inixsoftware – 2014-03-14T23:18:56.117

In bash, it is re-implemented as a built-in command. At a bash prompt type help [ – glenn jackman – 2014-03-15T00:13:31.193

Answers

7

This is test command.

For example, find out if /etc/passwd exists or not:

[ -f /etc/passwd ] && echo "Yes" || echo "No"

Regularly you write it as follows:

if test -f /etc/passwd
then
  echo "Yes"
else
  echo "No"
fi
Read 

stderr

Posted 2014-03-14T23:10:24.913

Reputation: 9 300

2

Have you ever seen/noticed if conditions in shell scripts? They are of the form:
[ -e $FILE ]

In those cases, the condition checking functionality is usually built-into the shell interpreting those scripts, usually bash or sh-aliased-bash.

The binaries that you see serve the same purpose, but are present so condition checks can be used in shells without such functionality built-in.

Pritam Baral

Posted 2014-03-14T23:10:24.913

Reputation: 156

2

The [ square bracket is another way to call the test command.

Refer to test's Man page for further info :)

kenkh

Posted 2014-03-14T23:10:24.913

Reputation: 199