💾 Archived View for sotiris.papatheodorou.xyz › gemlog › 20220802_re_misadventures_with_bash_shell.g… captured on 2024-08-18 at 17:21:29. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-01-29)

-=-=-=-=-=-=-

Re: Misadventures with bash shell

StackSmith voiced an understandable frustration with Bash scripts and its opaque syntax and wondered why people like it.

StackSmith's post

I have to agree that shell syntax is often weird, the error messages can be confusing, variable quoting isn't obvious and certain constructs are whitespace-sensitive. I encountered all of the problems StackSmith mentioned when I first started writing shell scripts and any one of them could have put me off from using or learning shell.

I'm not sure why I stuck with them but after properly learning about the shell most of the issues went away. I must admit though that even after learning about it, it's still a weird language with the potential for programs to fail in bad ways due to subtle bugs. Which is why I routinely pass my scripts to ShellCheck.

Why I write shell scripts

The main reason I write shell scripts is that I consider them the easiest and fastest way to glue together different programs. I think that pipelines, passing the output of one program as the input to another, is one of the most powerful and useful concepts in Unix-like operating systems. Pipelines allow combining multiple simple programs to perform more complicated tasks, kind of how you can use simple LEGO blocks to create impressive models.

The other reason I write shell scripts was actually mentioned by StackSmith. A shell interpreter is available on every Unix-like system. Especially if you stick to the POSIX shell specification, as opposed to Bash, your script will run correctly on both a desktop computer with the latest software and a 15 year old Linux-powered router.

Learning shell

I'm not suggesting everyone should learn shell but for me it was time well-spent. I believe this will also be the case for people working a lot with Unix-like system administration. For people wanting to learn more about shell I suggest the following resources:

A nice, comprehensive tutorial for the POSIX shell.

ShellCheck, a static analyzer for shell scripts.

Pure sh bible, a collection of POSIX shell snippets for performing common tasks.

Common Lisp Accordant SHell

I would be really interested in seeing a way to combine the good parts of shell like pipelines, input/output redirections and easily using any executable available in the system with the power and elegance of Lisp.

Sotiris 2022-08-02

Update later on 2022-08-02

JeanG3nie correctly pointed out that using POSIX shell is not enough for portability, you have to use POSIX utilities too.

JeanG3nie's post

What helps in this regards is installing the POSIX manpages, available on most Linux distros. Then instead of

man head

you can run

man 1p head

to read the POSIX specification of head.

If manpages aren't your thing you can always consult the fairly readable POSIX standard.

The POSIX.1-2017 standard

Of course if your system isn't POSIX compliant there's not much you can do other than complain to whoever is responsible. Standards are important.