By Shash


2012-03-06 09:36:22 8 Comments

I was trying to write a Bash script that uses an if statement.

if  [$CHOICE -eq 1];

The script was giving me errors until I gave a space after [ and before ] as shown below:

if  [ $CHOICE -eq 1 ];

My question here is, why is the space around the square brackets so important in Bash?

4 comments

@Johnsyweb 2012-03-06 09:39:48

Once you grasp that [ is a command, a whole lot becomes clearer!

[ is another way to spell "test".

help [

However while they do exactly the same, test turns out to have a more detailed help page. Check

help test

...for more information.


Furthermore note that I'm using, by intention, help test and not man test. That's because test and [ are shell builtin commands nowadays. Their feature set might differ from /bin/test and /bin/[ from coreutils which are the commands described in the man pages.

@Benoit 2012-03-06 09:41:30

and originally, it was at /usr/bin/[ and was a hardlink to /usr/bin/test, when invoked as [ you should include a closing brace as last argument. Now it's a shell builtin.

@William Pursell 2019-07-15 18:29:25

Note that they are not quite "exactly" the same. [ requires that its last argument be ].

@Jahid 2015-06-18 13:24:29

[ is a command and $CHOICE should be an argument, but by doing [$CHOICE (without any space between [ and $CHOICE) you are trying to run a command named [$CHOICE. The syntax for command is:

command arguments separated with space

@Cameron 2012-03-06 09:40:07

From another question:

A bit of history: this is because '[' was historically not a shell-built-in but a separate executable that received the expresson as arguments and returned a result. If you didn't surround the '[' with space, the shell would be searching $PATH for a different filename (and not find it) . – Andrew Medico Jun 24 '09 at 1:13

@Prince John Wesley 2012-03-06 09:41:10

[ is a test command. So it requires space.

Related Questions

Sponsored Content

65 Answered Questions

[SOLVED] How to get the source directory of a Bash script from within the script itself

  • 2008-09-12 20:39:56
  • Jiaaro
  • 1610917 View
  • 4742 Score
  • 65 Answer
  • Tags:   bash directory

35 Answered Questions

[SOLVED] How can I check if a program exists from a Bash script?

  • 2009-02-26 21:52:49
  • gregh
  • 632845 View
  • 2079 Score
  • 35 Answer
  • Tags:   bash

32 Answered Questions

[SOLVED] How do I split a string on a delimiter in Bash?

22 Answered Questions

[SOLVED] Echo newline in Bash prints literal \n

  • 2011-12-11 21:01:54
  • Sergey
  • 1963683 View
  • 2221 Score
  • 22 Answer
  • Tags:   bash echo newline

37 Answered Questions

[SOLVED] Extract filename and extension in Bash

30 Answered Questions

[SOLVED] How to concatenate string variables in Bash

23 Answered Questions

[SOLVED] How to check if a string contains a substring in Bash

  • 2008-10-23 12:37:31
  • davidsheldon
  • 1936373 View
  • 2399 Score
  • 23 Answer
  • Tags:   string bash substring

19 Answered Questions

[SOLVED] How do I tell if a regular file does not exist in Bash?

  • 2009-03-12 14:48:43
  • Bill the Lizard
  • 2437112 View
  • 3119 Score
  • 19 Answer
  • Tags:   bash file-io scripting

9 Answered Questions

[SOLVED] Why does ++[[]][+[]]+[+[]] return the string "10"?

  • 2011-08-26 08:46:14
  • JohnJohnGa
  • 199119 View
  • 1624 Score
  • 9 Answer
  • Tags:   javascript syntax

20 Answered Questions

[SOLVED] How do I iterate over a range of numbers defined by variables in Bash?

Sponsored Content