Explain Usage Of The Linux Left Bracket /usr/bin/[ Command

in Categories , , , last updated May 5, 2008

Q. I see [ command in /usr/bin directory. What is the /usr/bin/[ command used for?

A.This is test command and it is used to check file types and compare values.

General syntax is as follows:

For example, find out if /etc/passwd exists or not:
[ -f /etc/passwd ] && echo "Yes" || echo "No"
Regularly you write it as follows:

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


if test -f /etc/passwd 
   echo "Yes"
  echo "No"

Read test or [ command man page for all other expressions.
man [
man test

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Share this on (or read 1 comments/add one below):

1 comment

  1. Doesn’t work as written in all shells (e.g. BASH – not tested in others)

    root@localhost:~/root# ls -l
    total 0
    -rw——-. 1 root root 0 Oct 25 14:19 test

    root@localhost:~/root# [ -a test] echo “Y” || echo “N” // <– Fails
    bash: [: missing `]'

    root@localhost:~/root# [ -a test ]; echo "Y" || echo "N" // <– Requires ';' after ']'

    In Bash, the semi-colon ';' separates commands on the same line allowing multiple commands on a single line


    Have a question? Post it on our forum!