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

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:
[ EXPRESSION ]

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 ] 
then
   echo "Yes"
else
  echo "No"
fi

OR

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

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


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • NetScr1be Oct 25, 2015 @ 14:38

    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 `]'
    N

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

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

    –PB–

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum