Q: What is /bin/[ ?

In /bin I see this:
Code:
ls -l /bin
total 1298
-r-xr-xr-x  2 root  wheel   23672 Nov  1  2019 [
-r-xr-xr-x  1 root  wheel   23672 Nov  1  2019 cat
-r-xr-xr-x  1 root  wheel   15400 Nov  1  2019 chflags
-r-xr-xr-x  1 root  wheel   27672 Nov  1  2019 chio
-r-xr-xr-x  1 root  wheel   19488 Nov  1  2019 chmod
-r-xr-xr-x  1 root  wheel   27744 Nov  1  2019 cp
. . .

What is /bin/[?
 
test(1)

Code:
# ls -ali /bin/test /bin/\[
159227907 -r-xr-xr-x  2 root  wheel  12584 Jun 17 13:40 /bin/[
159227907 -r-xr-xr-x  2 root  wheel  12584 Jun 17 13:40 /bin/test
Both files have the same inode number, which means they're hardlinked.

These are syntactically the same:
Code:
[ -f /some/file ] && cat /some/file
Code:
test -f /some/file && cat /some/file
 
Yeah, in the early days it looked like shell "syntax" but was actually an external program.

I think most shells now provide the [ themselves rather than calling out to an external program.
That UNIX idea always seems really flexible but horribly inefficient to me.
 
I think most shells now provide the [ themselves rather than calling out to an external program.
As far as I know only bash and related do this, and they use the double notation for that ( [[ )
 
As far as I know only bash and related do this, and they use the double notation for that ( [[ )

Hmm, now I am not sure. Looking at the test manpage (https://www.freebsd.org/cgi/man.cgi?test)

"Some shells may provide a builtin test command which is similar or identical to this utility. Consult the builtin(1) manual page."

The builtin(1) manpage mentions that sh has a built in [ whereas csh doesn't. And the one on Linux:

"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."

I assumed this meant that use of the internal one was implicit.
 
Hmm, now I am not sure.
Great, now I'm not sure either :D

Ok, I was close with bash, it's the Bourne shell that has it builtin:
Code:
[       A built-in equivalent of test(1).
As bash is a Bourne shell derivative it stands to reason bash has it too. Likely other Bourne shells like zsh will too.
 
Si in test code, there is a code like :
if ($0 ='[') search the ']' character
But this is a test... :)
That made my day, thanks
 
RTFM man builtin builtin(1) for what is external and -- yes, built-in ;)
And use eval(1) in your scripts when the shell does not do what you want but what you told her, and you reach the point you feel like you could run out on the street and start a fight with the next innocent stranger who passes by occasionally...
 
and do not use bash scripts. It's not portable.

* Unless there is a good reason (some feature makes what you are doing much cleaner to do / more maintainable) and you understand the limitations. (I can’t recall the last time I ran into a system where bash wasn’t available, even if not installed by default.)
 
Back
Top