#!/bin/sh does not equal #!/bin/bash

104 views
Skip to first unread message

gary....@gmail.com

unread,
Sep 12, 2023, 1:49:48 AM9/12/23
to weewx-user
I use systemd timers for various things and after decommissioning a very old machine that used busybox, I recycled a cron script.

I could run the script from the cli, but it never ran via the timer.
Timer was fine, no errors were ever logged anywhere. The expected output just didn't occur.

Yesterday, I was looking through all my scripts adding comments to help me in the future.

When I opened the script that never runs, I found the problem.

#!/bin/sh does not equal #!/bin/bash

At least when calling a script via the service file fired by a systemd timer

Changed from sh to bash and voila! Change back to sh, fails silently again.

I'm sure >90% of the folks here knew this, but for those like me I figured I'd post.

How's this related to WeeWX? I use a systemd timer to backup the database. That one works a treat as I used bash, not sh

Nate Bargmann

unread,
Sep 12, 2023, 2:33:33 AM9/12/23
to weewx...@googlegroups.com
* On 2023 11 Sep 20:50 -0500, gary....@gmail.com wrote:
> How's this related to WeeWX? I use a systemd timer to backup the database.
> That one works a treat as I used bash, not sh

It really depends on the distribution. Some use Bash as the login and
system shell by symlinking it to /bin/sh. When called as 'sh' Bash
mostly restricts itself to POSIX defined sh behavior. So-called
Bashisms (Bash extensions to sh grammar) are turned off and scripts
written to take advantage of Bashisms will have errors.

Several releases ago Debian went one step further and set dash (Debian
Almquist Shell) as the default system shell as noted:

https://wiki.debian.org/Shell

Converting a script with Bashisms to dash (sh) can be trivial or not.
To aid in that process Debian offers checkbashisms:

https://manpages.debian.org/bookworm/devscripts/checkbashisms.1.en.html

Debian's choices filter down to Raspberry Pi OS and other derivatives
such as Armbian and Ubuntu flavors..

It is up to the script author/maintainer/distribution which shell to
support. Some distributions have their system shell scripts written
with the #!/bin/bash shebang. It is worth noting that beginning with
Debian 12 (Bookworm) Bash can no longer be set as the system shell
by running 'dpkg-reconfigure dash'. Of course manually editing the
symlink can be done but there may be unexpected results with system
scripts now written for Dash.

More than anyone wanted to know!

- Nate

--
"The optimist proclaims that we live in the best of all
possible worlds. The pessimist fears this is true."
Web: https://www.n0nb.us
Projects: https://github.com/N0NB
GPG fingerprint: 82D6 4F6B 0E67 CD41 F689 BBA6 FB2C 5130 D55A 8819

signature.asc

Cameron D

unread,
Sep 12, 2023, 12:28:18 PM9/12/23
to weewx-user
>>>  More than anyone wanted to know!  ??
Well, no - it is just what I wanted to know, only it's a bit too late.
The recent Debian 12 upgrade broke an embarrassingly large fraction of my cron scripts. Even though the scripts were #!/bin/sh, the bashisms were still working, so I had assumed they were posix-compatible.

Those links look like something I should investigate, thank you.
Cameron.

Greg Troxel

unread,
Sep 12, 2023, 6:02:42 PM9/12/23
to Nate Bargmann, weewx...@googlegroups.com
Nate Bargmann <n0...@n0nb.us> writes:

Thanks for your super complete and informative note.

> It is up to the script author/maintainer/distribution which shell to
> support. Some distributions have their system shell scripts written
> with the #!/bin/bash shebang. It is worth noting that beginning with
> Debian 12 (Bookworm) Bash can no longer be set as the system shell
> by running 'dpkg-reconfigure dash'.

It is up to the author, but I believe it is a bug to rely on non-POSIX
behavior at all. It is certainly a bug to do so without a Big Scary
Warning just after the #! that states that the script is non-portable
and which features to use.

Its' great of Debian to be helping to root out these bugs.

(FWIW, I'm not in general a bash hater -- I use it as my login shell --
but I do vigorously object to non-portable scripts. I use NetBSD which
has a almost-or-entirely POSIX-compliant sh without most bash
extensions.)
Reply all
Reply to author
Forward
0 new messages