3

I cannot run the strings command on the strings binary (which is for me located at /usr/bin/strings:

$ strings $(which strings)
strings

I also can't copy it and try it on the copy of it:

$ cp -L $(which strings) /tmp/
$ strings /tmp/strings
strings

The only way I can get it is to capture the output in cat:

$ strings <<< $(cat $(which strings))
...

But there are other commands that this kind of self-referencing works with (e.g., cat $(which cat)). I looked it up and it works on some other machines [image pulled from here]. Perhaps it is a macOS thing? (I am currently running macOS 10.14).

I understand that this is a silly question with potentially no use case, but I am just curious...

6
  • 1
    Use strings $(type -p strings). – waltinator Apr 23 at 3:41
  • I wonder what's the output of which strings on your system? On mine (Ubuntu 16), strings $(which strings) outputs the expected gibberish. – berndbausch Apr 23 at 3:42
  • @waltinator On my machine, strings $(type -p strings) and strings $(command -v strings) give the same output as strings $(which strings). – Jake Ireland Apr 23 at 3:43
  • @berndbausch /usr/bin/strings. It's not a symlink or anything either. File permissions are -rwxr-xr-x. Edit: in your answer to my other question, echo abc | strings doesn't work for me either; I have to run strings <<< $(echo abc). – Jake Ireland Apr 23 at 3:44
  • I am surprised but I have neither MacOS nor BSD to check. I don't know if your shell interprets $(...) differently than Bash? – berndbausch Apr 23 at 3:45
6

I got the same behavior in both zsh and bash in macOS. It turns out strings in macOS just looks in the non-text sections of the Mach-O binary. From man strings:

Unless the - flag is given, strings looks in all sections of the object files except the (__TEXT,__text) section. If no files are specified standard input is read.

So to force strings to treat the file as pure binary instead of Mach executable you need to pass the - flag. strings - "$(command -v strings)" will work as expected as long as you've not redefined string as a function.

In zsh, you can just use strings - =strings where =strings expands to the path of the strings command regardless of whether you've also got a function by that name or not (see also "$(command -ve strings)" in the yash shell).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.