5

So I know I can change she-bangs to #!/usr/bin/env bash and I will do that for my scripts, but really I'd like to be able to just run the many many scripts with a #!/bin/bash she-bang. Sometimes it's easy to change, sometimes it isn't, because the script is run by some other thing, not you directly, and it's somewhere you don't really want to touch.

Is there some option I can enable in my system config to get a link at /bin/bash, also is there any reason I shouldn't want this, why isn't this the standard? (And please don't tell me something about a slippery slope, /bin/bash is probably one of the most common assumptions to make about a Unix system)

1

2 Answers 2

6

The standard POSIX shell is sh, not bash. So you can assume POSIX systems will have a bourne-compatible shell available and called sh, but there is no reason to assume they will have bash installed, nor that it will be at /bin/bash. In fact, the POSIX specification doesn't even require sh to be /bin/sh:

Applications should note that the standard PATH to the shell cannot be assumed to be either /bin/sh or /usr/bin/sh, and should be determined by interrogation of the PATH returned by getconf PATH , ensuring that the returned pathname is an absolute pathname and not a shell built-in.

For example, to determine the location of the standard sh utility:

command -v sh

On some implementations this might return:

/usr/xpg4/bin/sh

Furthermore, on systems that support executable scripts (the "#!" construct), it is recommended that applications using executable scripts install them using getconf -v to determine the shell pathname and update the "#!" script appropriately as it is being installed (for example, with sed).

So the reason that /bin/bash isn't standard is that bash isn't standard. Which is precisely why env exists and why #!/usr/bin/env bash is the portable solution for this sort of thing.

That said, if it's your system, that's completely up to you. There is no reason not to add a symlink in /bin/bash pointing to wherever your bash is installed. If your bash is in, for example, /usr/bin, you could just open a root shell and run:

ln -s /usr/bin/bash /bin/bash

As long as you own the system, or you clear this with the local sysadmin, there should be no problem.

3
  • As a dirty workaround, I believe ln -s sh /bin/bash works OK on NixOS. There's probably a nicer way of setting that up in configuration.nix, but I'd say it will still remain a dirty workaround. Commented Feb 8, 2021 at 19:47
  • 2
    @VladimírČunát why would you do that? sh is not bash, so why would you try to treat it as bash? The OP already knows how to get an sh shell, the question is about getting bash instead. Anyway, doing it this way will not work if you try to run a non portable bash script.
    – terdon
    Commented Feb 8, 2021 at 20:36
  • On (my) NixOS /bin/sh seems to be bash. The binary knows in which mode to run by inspecting argv[0]. Just try setting this up and running /bin/bash --version. Commented Feb 9, 2021 at 8:29
1

For now the default shell on NixOS is Bash. Assuming you have not changed that then you can do the following in /etc/nixos/configuration.nix.

system.activationScripts.binbash = {
    deps = [ "binsh" ];
    text = ''
         ln -s /bin/sh /bin/bash
    '';
  };
2
  • Nice. I guess there should also be a way to get the path to bash in /nix. But this seems already nicer than just doing it manually.
    – Julia Path
    Commented Oct 7, 2023 at 9:38
  • Yes, I'm not sure what the best way to do it would be, but I think the definition of binsh shows the way: github.com/NixOS/nixpkgs/blob/… Commented Oct 12, 2023 at 12:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .