Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I actually thought [ and test were symlinks, but that might just be from Alpine Linux where they're both symlinks to busybox.


Hardlinks actually, but who's counting? plus in real world usage you will hit the shell builtin anyway.

  ls -li /bin/\[ /bin/test
  26016 -r-xr-xr-x  2 root  bin  133256 Mar 25  2023 /bin/[
  26016 -r-xr-xr-x  2 root  bin  133256 Mar 25  2023 /bin/test
I have to admit I avoid "[" in my scripts, it is a weird hack trying to make a command look like syntax and this really bothers me for some reason.


No, Alpine uses symlinks:

    $ docker run --rm -ti alpine
    / # ls -l /usr/bin/test /usr/bin/[
    lrwxrwxrwx    1 root     root            12 Sep 28 11:18 /usr/bin/[ -> /bin/busybox
    lrwxrwxrwx    1 root     root            12 Sep 28 11:18 /usr/bin/test -> /bin/busybox
    / # ls -li /usr/bin/test /usr/bin/[
     495254 lrwxrwxrwx    1 root     root            12 Sep 28 11:18 /usr/bin/[ -> /bin/busybox
     495360 lrwxrwxrwx    1 root     root            12 Sep 28 11:18 /usr/bin/test -> /bin/busybox
    / #


Interesting... Doubly so because I thought busybox was a sort of linux crunchgen, A way to pack many independent executables into one to save space. With crunchgen at least, each executable name is then linked to the packed binary, that is, hardlinks. and the filesystem name is used to pick the correct code to run. Why did they go with softlinks? my guess... It can be moved across filesystem boundaries. Perhaps interference from cgroups?

http://man.openbsd.org/crunchgen


Both just look at the first entry in argv. Whether you used a hard- or symlink isn't very important for that. Or at least, that's how FreeBSD crunchgen works. Maybe they changed that in OpenBSD?


This is different. Alpine uses busybox, where everything is implemented in the busybox binary, thus everything is symlinked to it.

In other distributions, test is a separate binary, and [ is a link.


What bothers me is that you can write equivalent code with `test` and `[`, but when using `[` you need to terminate your conditional expression with `]`. Why? Isn't it the same binary? Why is the shell adding this extraneous requirement, just to surface "syntax errors" rather than simply treating `]` as a no-op?


It is the test command that requires the terminating ] when it is invoked as [.

It's possible your shell pre-empts this requirement and presents it as a proper syntax error, but it would have failed anyway.


They are different binaries on Linux Mint!

  # file /bin/test /bin/[
  /bin/test: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=6fe552c80ab0b3d3e60de2ab09167329e222eb67, for GNU/Linux 3.2.0, stripped
  /bin/[:    ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=99cfd563b4850f124ca01f64a15ec24fd8277732, for GNU/Linux 3.2.0, stripped

  #/bin/test --version
  #/bin/[ --version
  [ (GNU coreutils) 8.30
  Copyright (C) 2018 Free Software Foundation, Inc.
  License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
  This is free software: you are free to change and redistribute it.
  There is NO WARRANTY, to the extent permitted by law.

  Written by Kevin Braunsdorf and Matthew Bradburn.
Built with LBRACKET or not ...

https://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=blob;...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: