[ is an actual bash command (#tilPost)

I came across a tweet by Guillermo Rauch. He shared this little snippet.
$ which [
/bin/[

What’s that? I rarely do bash scripting so I’m always happy to learn more about it.
Guillermo was also so kind to also share a doc explaining…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

I came across a tweet by Guillermo Rauch. He shared this little snippet.

$ which [
/bin/[

What's that? I rarely do bash scripting so I'm always happy to learn more about it.

Guillermo was also so kind to also share a doc explaining this. If you prefer the TL;DR... here we go:

In bash there is a test command. You can use it to e.g. check if a file exists (with the -e flag) or if a file path points to a directory (with the -d flag). There are also many more options available and you can have a look executing man test. In bash scripts you probably use test inside of an if condition. So how does an if condition looks like in bash?

if test -e /etc/passwd; then
  echo "Alright man..." >&2
else
  echo "Yuck! Where is it??" >&2
  exit 1
fi

I found the example above online and well... this puzzled me. I always assumed that if conditions have to include [] in bash. And that's my learning today!

It turns out that [] are not bash syntax but an actual bash command.

$ which [

[: shell built-in command

$ man [
TEST(1)                   BSD General Commands Manual                  TEST(1)
NAME
     test, [ -- condition evaluation utility

SYNOPSIS
     test expression
     [ expression ]
...  
...

There are even manual pages for it but you see that the displayed man pages are the man pages of test. So this means that [ and test are actual the same thing. ?

$ test -e /does/not/exist
FAIL: 1

$ [ -e /does/not/exist ]
FAIL: 1

Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2017-09-05T22:00:00+00:00) [ is an actual bash command (#tilPost). Retrieved from https://www.scien.cx/2017/09/05/is-an-actual-bash-command-tilpost/

MLA
" » [ is an actual bash command (#tilPost)." Stefan Judis | Sciencx - Tuesday September 5, 2017, https://www.scien.cx/2017/09/05/is-an-actual-bash-command-tilpost/
HARVARD
Stefan Judis | Sciencx Tuesday September 5, 2017 » [ is an actual bash command (#tilPost)., viewed ,<https://www.scien.cx/2017/09/05/is-an-actual-bash-command-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » [ is an actual bash command (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2017/09/05/is-an-actual-bash-command-tilpost/
CHICAGO
" » [ is an actual bash command (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2017/09/05/is-an-actual-bash-command-tilpost/
IEEE
" » [ is an actual bash command (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2017/09/05/is-an-actual-bash-command-tilpost/. [Accessed: ]
rf:citation
» [ is an actual bash command (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2017/09/05/is-an-actual-bash-command-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.