Subject: Resources for learning shell scripting

From: Paul Zillmann <[email-masked]>
Date: Tue, 19 Nov 2024 15:57:42 +0100

Hello there,

recently I have to write a bit of shell scripts and I find myself with 
the problem of finding solutions online that are compatible with GNU 
bash, but not with busyboxes POSIX shell.
So I would spend a bit of time to adapt those scripts to be able to run 
with /bin/ash.

Could you recommend any good resources to learn how to script for POSIX 
in the first place?
Are there any good websites, books or even man pages available?

I'm not a very good bash scripter myself, usually I work with compiled 
languages like C and Golang.

Thanks,
- Paul

---

From: Silas <[email-masked]>
Date: Tue, 19 Nov 2024 12:14:42 -0300

On Tue, Nov 19, 2024 at 03:57:42PM +0100, Paul Zillmann wrote:
>Could you recommend any good resources to learn how to script for 
>POSIX in the first place?

Hello.

Reading different Bourne shell implementations man pages can help a 
lot. I recommend NetBSD and OpenBSD sh(1) man page.

There may be some Alpine specific software written in Bourne shell 
(openrc? which is actually not Alpine specific...), but I not too 
familiar with Alpine to recommend any.

Reading good implementations of programs writting in Bourne shell can 
help as well. On the top of my head, I remember acme.sh and NetBSD 
postinstall programs can be good refereces.

There may be other good resources on the web.  Some examples:

http://www.shelldorado.com/

David Wheeler's articles about shell security, like this one: 
https://dwheeler.com/essays/filenames-in-shell.html

---

From: Paul Zillmann <[email-masked]>
Date: Tue, 19 Nov 2024 16:37:59 +0100

> lot. I recommend NetBSD and OpenBSD sh(1) man page.

I almost forgot that with anything POSIX I can check out Unix' resources.
Might also be a good keyword - thank you.

- Paul

---

From: Unicorn <[email-masked]>
Date: Tue, 19 Nov 2024 17:26:50 +0100 (GMT+01:00)

Hi!

I can recommend https://shellhaters.org as a quick way to find all the 
POSIX documentation, both for shell syntax and all the 
commands/utilities. I use it all the time :)

Best,
Edin

---

From: Edward Miles Stapleton <[email-masked]>
Date: Tue, 19 Nov 2024 09:19:09 -0800

> Are there any good websites, books or even man pages available?

I've seen decent reviews for *Classic Shell Scripting*:
Arnold Robbins & Nelson H.F. Beebe, *Classic Shell Scripting* (O'Reilly, 2005)

---

From: Edward Miles Stapleton <[email-masked]>
Date: Tue, 19 Nov 2024 09:27:29 -0800

> Are there any good websites, books or even man pages available?

there's also the `shellcheck` tool (in the community repository):
https://github.com/koalaman/shellcheck

---

From: Steffen Nurpmeso <[email-masked]>
Date: Tue, 19 Nov 2024 20:03:40 +0100

Paul Zillmann wrote in
 <[email-masked]>:
 |recently I have to write a bit of shell scripts and I find myself with 
 |the problem of finding solutions online that are compatible with GNU 
 |bash, but not with busyboxes POSIX shell.
 |So I would spend a bit of time to adapt those scripts to be able to run 
 |with /bin/ash.
 |
 |Could you recommend any good resources to learn how to script for POSIX 
 |in the first place?
 |Are there any good websites, books or even man pages available?
 |
 |I'm not a very good bash scripter myself, usually I work with compiled 
 |languages like C and Golang.

Sven Mascheck is "an instance" regarding history of shell and
compatibility, used even by shell standard developers like Chet
Ramey (bash), dear Jörg Schilling (bosh), and other shell experts
like Stephane Chazelas:

  https://www.in-ulm.de/~mascheck/

Other than that it is a good idea to write scripts in shells which
are dedicated to POSIX compatibility, like dash.

(I have no idea of those "shell-check" tools which exist, many
swear by them.)

--steffen
|
|Der Kragenbaer,                The moon bear,
|der holt sich munter           he cheerfully and one by one
|einen nach dem anderen runter  wa.ks himself off
|(By Robert Gernhardt)
|
|And in Fall, feel "The Dropbear Bard"s ball(s).
|
|The banded bear
|without a care,
|Banged on himself fore'er and e'er
|
|Farewell, dear collar bear

---

From: Richard Brooksby <[email-masked]>
Date: Tue, 19 Nov 2024 20:34:59 +0000

> Could you recommend any good resources to learn how to script for POSIX

I highly recommend a habit of running your scripts through shellcheck 
<https://www.shellcheck.net/>, which is probably available from the 
package system of your distro (e.g. "apt-get install shellcheck").

I've been writing shell scripts for decades and it still catches my 
mistakes.  It gives good and specific advice.  And investigating *why* 
it gives advice is great for learning.

> languages like C and Golang.
Shells have grown rather organically and have fewer unifying principles 
than those languages.  Be prepared for weirdness.

---

From: [email-masked]
Date: Wed, 20 Nov 2024 06:40:20 +0100 (CET)

You should either try guide.bash.academy or https://tldp.org/LDP/abs/html/. Both are very good and practical.

Cheers.

19 Nov 2024, 20:28 by [email-masked]:

> Hello there,
>
> recently I have to write a bit of shell scripts and I find myself with the problem of finding solutions online that are compatible with GNU bash, but not with busyboxes POSIX shell.
> So I would spend a bit of time to adapt those scripts to be able to run with /bin/ash.
>
> Could you recommend any good resources to learn how to script for POSIX in the first place?
> I'm not a very good bash scripter myself, usually I work with compiled languages like C and Golang.
>
> Thanks,
> - Paul
>

---

From: Ralf Mardorf <[email-masked]>
Date: Wed, 20 Nov 2024 06:59:41 +0100

On Wed, 2024-11-20 at 06:40 +0100, [email-masked] wrote:
> You should either try guide.bash.academy
> or https://tldp.org/LDP/abs/html/. Both are very good and practical.

The “Advanced Bash Scripting Guide” is far from what the OP is asking
for! Bash provides arrays and so on.

Bashisms are frowned upon for portability.