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 before and 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?


@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

67 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
  • 1756941 View
  • 5036 Score
  • 67 Answer
  • Tags:   bash directory

37 Answered Questions

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

  • 2009-02-26 21:52:49
  • gregh
  • 709852 View
  • 2262 Score
  • 37 Answer
  • Tags:   bash

20 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
  • 2546857 View
  • 3302 Score
  • 20 Answer
  • Tags:   bash file-io scripting

26 Answered Questions

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

35 Answered Questions

[SOLVED] How can I check if a directory exists in a Bash shell script?

  • 2008-09-12 20:06:25
  • Grundlefleck
  • 2819704 View
  • 3768 Score
  • 35 Answer
  • Tags:   bash shell unix posix

23 Answered Questions

[SOLVED] Echo newline in Bash prints literal \n

  • 2011-12-11 21:01:54
  • Sergey
  • 2197788 View
  • 2454 Score
  • 23 Answer
  • Tags:   bash echo newline

32 Answered Questions

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

37 Answered Questions

[SOLVED] Extract filename and extension in Bash

30 Answered Questions

[SOLVED] How to concatenate string variables in Bash

20 Answered Questions

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

Sponsored Content