this post was submitted on 15 Nov 2023
787 points (96.1% liked)

Programmer Humor

19817 readers
47 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 21 points 1 year ago* (last edited 1 year ago) (1 children)

Just don't call it with #!/bin/sh. Because that's POSIX shell, not bash.

[–] [email protected] 5 points 1 year ago* (last edited 1 year ago) (6 children)

but effectively it's bash, I think /bin/sh is a symlink to bash on every system I know of...

Edit: I feel corrected, thanks for the information, all the systems I used, had a symlink to bash. Also it was not intended to recommend using bash functionality when having a shebang !#/bin/sh. As someone other pointed out, recommendation would be #!/usr/bin/env bash, or !#/bin/sh if you know that you're not using bash specific functionality.

[–] [email protected] 21 points 1 year ago* (last edited 1 year ago) (2 children)

Still don't do this. If you use bash specific syntax with this head, that's a bashism and causes issues with people using zsh for example. Or with Debian/*buntu, who use dash as init shell.

Just use #!/bin/bash or #!/usr/bin/env bash if you're funny.

[–] [email protected] 8 points 1 year ago (1 children)

#!/bin/bash doesn't work on NixOS since bash is in the nix store somewhere, #!/usr/bin/env bash resolves the correct location regardless of where bash is

[–] [email protected] 3 points 1 year ago* (last edited 1 year ago) (3 children)

Are there any distos with /usr/bin/env in a different spot? I still believe that's the best approach for getting bash.

[–] [email protected] 6 points 1 year ago (1 children)

All posix-compliant distros need /usr/bin/env

[–] [email protected] 5 points 1 year ago* (last edited 1 year ago) (1 children)

I do think a simple symlink is superior to a tool parsing stuff. A shame POSIX choose this approach.

Still the issue that a posix shell can be on a non-posix system and vice versa. And certificates versus used practice. Btw, isn't there only one posix certified Linux distro? Was it Suse?

[–] [email protected] 2 points 1 year ago

Posix certification is dumb but posix compliance is nice to ensure some level of compatibility.

Symlinks would be pretty bad in the case of nixos. Wouldn't fit at all

[–] [email protected] 2 points 1 year ago

My own. I use arch btw

[–] [email protected] 0 points 1 year ago (1 children)

/bin/bash won't work on every system for example NixOS some other systems may have bash in /usr/bin or elsewhere

[–] [email protected] 1 points 1 year ago (1 children)
[–] [email protected] 1 points 1 year ago

Binaries are not in /usr/bin or /bin except for /bin/sh and /usr/bin/env. Programs should not assume fixed paths for binaries and instead look for them in $PATH.

[–] [email protected] 15 points 1 year ago

No no no no no, do not believe this you will shoot yourself in the foot.

https://wiki.debian.org/Shell

Beginning with DebianSqueeze, Debian uses Dash as the target of the /bin/sh symlink. Dash lacks many of the features one would expect in an interactive shell, making it faster and more memory efficient than Bash.

From DebianSqueeze to DebianBullseye, it was possible to select bash as the target of the /bin/sh symlink (by running dpkg-reconfigure dash). As of DebianBookworm, this is no longer supported.

[–] [email protected] 5 points 1 year ago

It is a symlink, but bash will automatically enable posix compliance mode if you use it. So any bash specific features will bomb out unless you explicitly reset it in the script.

[–] [email protected] 4 points 1 year ago* (last edited 1 year ago)

Wut that is not even the case for Ubuntu. You're probably thinking of dash example:

sh -c '[[ true ]] && echo ya' 
# sh: 1: [[: not found

bash -c '[[ true ]] && echo ya' 
# ya
[–] [email protected] 3 points 1 year ago* (last edited 1 year ago)

i thought most unix-like systems had it symlinked to a shell like dash. it's what i have on my system (void linux), of course not as an interactive shell lol

i use #!/bin/sh for posix scripts and #!/usr/bin/env bash for bash scripts. #!/bin/sh works for posix scripts since even if it's symlinked to bash, bash still supports posix features.

[–] [email protected] 1 points 1 year ago

macOS
Debian
Ubuntu