In today's #Linux #shell adventures, did you know that [ is an executable ELF binary provided by coreutils? There's a /usr/bin/[ program in the $PATH on most Linux systems.
@alilly OK, so I dove into the rabbit hole and you're half right. In an interactive shell, the coreutils ELF binary is invoked, but in a *bash script*, the bash built-in gets invoked instead, which is the alias for the test bash built-in. Similarly, there is a coreutils for test as well and it behaves the same way.
@alilly @me So, I did this, and it's not calling test at all. It is calling libc and locale when it generates an error message. It seems like a basic and straightforward C program AFAICT.
@Jonathan Lamothe @Scott Williams 🐧 On my system both are their own binaries, and both seem to be different (vbindiff shows differences), but they do the same thing
Yeah, this one really made me do a double take when I noticed it many years down the road. Another one of those UNIX things that makes you think, “wait really??”
Then you remember other weird UNIX things and think, “yeah, well I guess I’m not that surprised at this particular weirdness.”
@alilly Writing my #qbsh side project has given me a new appreciation for Linux internals, coreutils, and built-ins - and not to mention the pipe layer between them all. It's a silly project, but I've definitely learned some useful things along the way.
@aburka But according to strace, the /usr/bin/[ is used when it's at the start of the command. I'm guessing that the bash built-in kicks in following if.
Jonathan Lamothe
in reply to Scott Williams 🐧 • •@Scott Williams 🐧 ...which is usually a symlink to
/usr/bin/test
, if I'm not mistaken.Edit: It appears this may no longer be the case.
Scott Williams 🐧
in reply to Jonathan Lamothe • • •Jonathan Lamothe
in reply to Scott Williams 🐧 • •Scott Williams 🐧 likes this.
Scott Williams 🐧
in reply to Jonathan Lamothe • • •Jonathan Lamothe likes this.
Athena L.M.
in reply to Scott Williams 🐧 • • •Scott Williams 🐧
in reply to Athena L.M. • • •@alilly @me Something, something, posix, something.
On that note `man posix` tells you about a Perl module.
Athena L.M.
in reply to Scott Williams 🐧 • • •strace
time!Scott Williams 🐧
in reply to Athena L.M. • • •silverwizard
in reply to Jonathan Lamothe • • •Jonathan Lamothe
in reply to silverwizard • •/usr/bin/test
Doesn't look for a trailing]
, for starters. ;)like this
Carl and Scott Williams 🐧 like this.
Ryan Frishkorn
in reply to Scott Williams 🐧 • • •Yeah, this one really made me do a double take when I noticed it many years down the road. Another one of those UNIX things that makes you think, “wait really??”
Then you remember other weird UNIX things and think, “yeah, well I guess I’m not that surprised at this particular weirdness.”
Athena L.M.
in reply to Scott Williams 🐧 • • •ls
isn't a builtin?Scott Williams 🐧
in reply to Athena L.M. • • •Scott Williams 🐧
in reply to Scott Williams 🐧 • • •aburka 🫣
in reply to Scott Williams 🐧 • • •Scott Williams 🐧
in reply to aburka 🫣 • • •aburka 🫣
in reply to Scott Williams 🐧 • • •Scott Williams 🐧
in reply to aburka 🫣 • • •Scott Williams 🐧
in reply to Scott Williams 🐧 • • •Scott Williams 🐧
in reply to Scott Williams 🐧 • • •@aburka OK, I think I've figured it out. When running in an interactive shell, the coreutils version is invoked.
When running in a script, the bash built-in is invoked regardless of whether at the beginning of the line or followed by if.
Carl
in reply to Scott Williams 🐧 • • •