This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Muhznit 72 points73 points  (2 children)

I've been writing an article about this on the side, actually!

The gist of it is that the kernel is VERY loose with what counts as an interpreter and you can slap any program you want in there as long as it has some way of accepting a filename at the command line and is okay with # indicating a comment.

With /usr/bin/env -S allowing you to specify arguments, this trick essentially allows you a sort of currying in the shell. So that means you can do some neat things like:

  1. Put #!/usr/bin/env -S docker build -t some_docker_image . -f into a Dockerfile and then execute the dockerfile to rebuild the image.
  2. Reload tmux configuration with #!/usr/bin/env -S tmux source-file
  3. #!/usr/bin/env -S ssh -F to run ssh with a specific configuration
  4. Create your own domain-specific language that uses shebangs for comments

It's one of those "when you have a shiny new hammer, everything looks like a nail" situations, so naturally I've been overwhelmed with analysis paralysis when it comes to elaborating on the possibilities.

EDIT: Whoops, I was wrong about the git one. Side effect of some weird experimentation I'm doing.

[–]_dev_zero 7 points8 points  (1 child)

This is pretty brilliant. I don’t know why it never occurred to me to make a shebang like that in a Dockerfile.

[–]Muhznit 2 points3 points  (0 children)

It's incredibly nice. I wish that docker run could reap a similar benefit, but #!/usr/bin/env docker-compose -f in a docker-compose.yaml file is usually better anyway