DEV Community

loading...
Cover image for A "POSIX Playground" Container for Shell Script Testing

A "POSIX Playground" Container for Shell Script Testing

Jonathan Bowman
Constantly learning to develop software. A Python enthusiast. Works at Candoris, helping clients use Salesforce effectively. Want to buy me coffee? ko-fi.com/bowmanjd
・2 min read

After exploring portable scripting in the previous article, I built a container available on Docker Hub, useful for testing and experimentation.

The container is an Alpine Linux (edge) container with the following tools:

  • posh shell (this is the default)
  • dash shell
  • rlwrap to offer readline support for either of the above shells (rlwrap posh is the default command if none specified)
  • checkbashisms for rooting out Bash-specific idiosyncrasies in scripts
  • shellcheck for shell script linting

See the "Writing Bash Scripts that are not only Bash" for explanation and usage of the above.

Using the container

(Podman also works fine in any of the examples below.)

First, pull the image:

docker pull docker.io/bowmanjd/posix-playground
Enter fullscreen mode Exit fullscreen mode

To launch posh: Policy-compliant Ordinary SHell with readline support:

docker run -it -v "$PWD:/work" posix-playground
Enter fullscreen mode Exit fullscreen mode

To instead launch dash shell with readline support:

docker run -it -v "$PWD:/work" posix-playground rlwrap dash
Enter fullscreen mode Exit fullscreen mode

(Without rlwrap, line editing in either of these shells is impossible, making experimentation rather painful. Using rlwrap is recommended.)

To check a helloworld.sh shell script in the current directory for "bashisms" (make sure the first line, the "shebang", reads #!/bin/sh):

docker run -it -v "$PWD:/work" posix-playground checkbashisms helloworld.sh
Enter fullscreen mode Exit fullscreen mode

To check a helloworld.sh shell script in the current directory for a variety of gotchas:

docker run -it -v "$PWD:/work" posix-playground shellcheck helloworld.sh
Enter fullscreen mode Exit fullscreen mode

To simply run a script from the current directory, using posh:

docker run -it -v "$PWD:/work" posix-playground posh myscript.sh
Enter fullscreen mode Exit fullscreen mode

Posh: an obscure shell with great usefulness

Of the tools included in the image, posh is the winner for me. It is not available on every distro, so having a container available is quite convenient.

Posh is also quite strict, and this is the point. It does not have any syntactic sugar to make it more Bash-like. I feel quite reassured when a script runs correctly in posh.

If you have other tools to recommend, or want to reflect on your experiences with this container image, feel free to post in the comments!

Discussion (0)

Forem Open with the Forem app