Hacker News new | ask | show | jobs
by liraz 5927 days ago
I find using specific shebangs is a good habit if you don't know exactly which generic POSIX shell features/syntax you can rely on. In years past I would all too often use #!/bin/sh for the shebang only to find out later I had accidentally used a BASH specific shell feature, which is fine if /bin/sh happens to point to /bin/bash but can break if it points to something else.

OTOH, I believe the error handling tricks discussed should work with any POSIX shell so I've updated the shebang line in the code snippets to use the more generic #!/bin/sh.

2 comments

I agree with using /bin/bash if that's what you're actually using and aren't sure the script will work more generally.

Something that can help pin down usage of bash extensions is checkbashisms (in the devscripts package on Debian systems). Also running the script through a shell that claims POSIX compliance like /bin/dash.

When invoked as sh, Bash enters POSIX mode after reading the startup files.
When invoked that way it still supports more than /bin/dash on my system:

  % ln -s /bin/bash sh
  % ./sh
  sh-3.2$ echo {1..10}
  1 2 3 4 5 6 7 8 9 10
  sh-3.2$ exit
  % dash
  $ echo {1..10}
  {1..10}
where dash is supposed to be POSIX compliant.