What is the use of ‘/usr/bin/[‘ [Square Bracket] Command in Linux?

Updated on September 3, 2017

Today I came across an interesting command ‘[‘ (square bracket) via the famous Linux blog called nixCraft. It was quite an interesting post that explained the use of ‘/usr/bin/[‘ command in Linux. But you know what, I have been working in Linux for few years now and I didn’t know this command until that tweet from nixcraft popped up in my news feed. I learned it from you guys, thanks.

Well, I’m writing this post to share what I learnt from nixCraft. Basically ‘/usr/bin/[‘ command is equal to the ‘test‘ command in Linux. Let me explain.

In shell programming, if you ever want to check whether a particular file exists or not, then you normally use IF cond isn’t? Something like the one shown below:

if [ -f /etc/passwd ]
then
   echo "Yes"
else
  echo "No"
fi

Now, if ‘/etc/passwd‘ exists, then you see the output “Yes” else “No“. That’s simple and you can do the same to compare values as well. But did you note that ‘[‘ bracket after ‘if‘? That’s where is’/usr/bin/[‘ comes into the scene.

Alternatively, you can check whether file exists or not as below (without IF):

[demo ~]$ [ -f /etc/passwd ] && echo "Yes" || echo "No"
Yes
[demo ~]$ [ -f /etc/passwd1 ] && echo "Yes" || echo "No"
No

In the above snippet, you can see ‘[‘ command. As told, the command is equal to ‘test‘. Checkout the below example:

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

Ok, both ‘[‘ and ‘test‘ does the same job, but are they symlinked? I checked that as well, but they are not.

[demo ~]$ ls -l /usr/bin/test
 -rwxr-xr-x 1 root root 30064 Mar 21 2012 /usr/bin/test
[demo ~]$ ls -l /usr/bin/[
 -rwxr-xr-x 1 root root 32296 Mar 21 2012 /usr/bin/[

Basically, ‘[‘ is a builtin shell command. Confirm it using ‘type‘ command as below:

[demo ~]$ type -a [
[ is a shell builtin
[ is /usr/bin/[

Note: You see ‘[‘ is a builtin command in Linux. It means, if the command contains no slashes, then SHELL tries to locate it via PATH variable.

Since, it’s a command, there should be a man page isn’t?

[demo ~]$ man [
 No manual entry for [

I couldn’t find man page for ‘[‘, then I saw the man page of ‘test‘ where at the bottom, there was a Note that says about ‘[‘. See below:

[demo ~]$ man test
:::::::::::::
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.

Here’s an interesting tweet related to this from Joe Walnes.

Linux command [

Bonus…Like ‘[‘ there’s ‘[[‘ as well

[demo ~]$ type -a [[
[[ is a shell keyword

[[‘ is actually a keyword similar to other keywords in SHELL – ‘! until do done select elif else esac fi for case function if [[ ]] in then while { } time

Since it’s a keyword, you cannot find that as a command.

[demo ~]$ ls -l /usr/bin/[[
ls: /usr/bin/[[: No such file or directory
[demo@gridfs ~]$ whereis [[
[[:

Was this article helpful?

Related Articles

Leave a Comment