Robert Elder Software Inc.
  • Home
  • Store
  • Blog
  • Contact
  • Home
  • Store
  • Blog
  • Contact
  • #linux
  • |
  • #commandline
  • |
  • #softwareengineering
  • |
  • #embeddedsystems
  • |
  • #compilers
  • ...
  • View All >>

Intro To '[' Command In Linux

2023-08-09 - By Robert Elder

     I use the 'opening left square bracket' command as a syntactically convenient substitute for the 'test' command:

[ 0 ]
echo $?
0

The '[' Can Be An Executable

     I can write a single-line if statement using the square bracket command like this:

if [ 5 -gt 3 ]; then echo "a"; else echo "b"; fi
a

     or like this:

if /usr/bin/[ 5 -gt 3 ]; then echo "a"; else echo "b"; fi
a

     or I can use the 'test' command like this:

if /usr/bin/test 5 -gt 3; then echo "a"; else echo "b"; fi
a

     In both scenarios, the expression to evaluate and the closing square bracket are simply passed like regular arguments to the test command executable.

Executable Program Or Shell Built-In?

     In practice, the '[' command can be confusing due to the fact that the 'test' and the square bracket commands can both exist as built-in shell features, or as stand-alone executables:

type -a [
[ is a shell builtin
[ is /usr/bin/[
[ is /bin/[
type -a test
test is a shell builtin
test is /usr/bin/test
test is /bin/test
which [
/usr/bin/[
which test
/usr/bin/test

     In the GNU Coreutils implementation of the 'test' command, you can see that the 'test' and the '[' commands are implemented by the same piece of source code.

     Furthermore, you can see that the man pages of these two commands are completely identical:

vim -d <(man [) <(man test)

     And that's why the '[' command is my favourite Linux command.

Intro To 'stty' Command In Linux
Intro To 'stty' Command In Linux
Published 2023-10-04
Terminal Block Mining Simulation Game
$1.00 CAD
Terminal Block Mining Simulation Game
Intro To 'nproc' Command In Linux
Intro To 'nproc' Command In Linux
Published 2023-07-15
Intro To 'comm' Command In Linux
Intro To 'comm' Command In Linux
Published 2023-09-06
How To Force The 'true' Command To Return 'false'
How To Force The 'true' Command To Return 'false'
Published 2023-07-09
A Surprisingly Common Mistake Involving Wildcards & The Find Command
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01
Intro To 'chroot' Command In Linux
Intro To 'chroot' Command In Linux
Published 2023-06-23
Join My Mailing List
Privacy Policy
Why Bother Subscribing?
  • Free Software/Engineering Content. I publish all of my educational content publicly for free so everybody can make use of it.  Why bother signing up for a paid 'course', when you can just sign up for this email list?
  • Read about cool new products that I'm building. How do I make money? Glad you asked!  You'll get some emails with examples of things that I sell.  You might even get some business ideas of your own :)
  • People actually like this email list. I know that sounds crazy, because who actually subscribes to email lists these days, right?  Well, some do, and if you end up not liking it, I give you permission to unsubscribe and mark it as spam.
© 2025 Robert Elder Software Inc.
SocialSocialSocialSocialSocialSocialSocial
Privacy Policy      Store Policies      Terms of Use