Follow

So obviously not every system sets to /bin (such as )

If two systems have different install paths, say:
System1:
/home/me/bin/bash
System2:
/bin/bash

How can I write a shell script that allows both systems to be happy? can I do:
#!$BASH
#!which bash
or something across these lines?

I really need to learn Bash properly sometime....

@MutoShack what about using something like #!/usr/bin/env bash ?

@constrict0r

It's weird - GuixSD doesn't come with a /usr directory, it has a /gnu directory (I can add a /usr/env if I wanted to, but I want my software to be as out-of-the-box as possible, without the need for userside customization).

Thanks, though! /usr/bin/env, although not a catch-all in my case, is way more portable than the usual (and sadly ubiquitous) #!/bin/bash

@MutoShack well GuixSD adventures is a game that I hope to play soon

@constrict0r @MutoShack The location of env is of course a killer. /bin/env is (annoyingly) a thing.

@gemlog

You mean I can do that in the script itself? Like
#! $PATH/bash
?

I'm not trying to make this script run on two of *my* systems, per se, but on as many machines as possible. I'm wondering what the most portable way to begin a shell script would be. I see
#!/bin/bash
a lot, but I hate it since my machine has a different location for bash!

@MutoShack No. You can set multiple locations in the path variable for where it searches, in order.
If the actual location of bash itself varies across systems, you're a bit effed.
Here's the one on my home box as an example.

immaculata 15-20:01 ~> echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/gemlog/bin/vc:/home/gemlog/bin:/home/gemlog/bin:/home/gemlog/bin/vc:/home/gemlog/bin

@gemlog @MutoShack Shebangs require full path to the interpreter, maybe followed by some options.
$PATH and other environment variables may help with wrapping the interpreters into an `env`, but the path to the `env` itself is still hardwired.
Guix and Nix developers couldn't come up with anything better than patch the shebangs or guarantee the paths are actual.

@MutoShack does patch the shebangs in all the scripts, so they always point to the specific path in the store.
While is more lax and just keeps the standard path for the shell (not a good idea actually).

Generally, you write a script with some standard path (#!/bin/sh), and a Guix/Nix expression that would be used to run it in some specific environment (e.g. `guix build` and `nix-build`), and point it to the correct shell by the way.

Sign in to participate in the conversation
Functional Café

functional.cafe is an instance for people interested in functional programming and languages.