Search code examples
linuxbashintegration-testingversioning

How can I test my Bash script on older versions of Bash?


I'm working on a Bash library and want to ensure I'm supporting as many environments as possible - including old installations of Bash. My development environment is Bash 4.3, but some of my users may well be running much older versions and presently I have no way to confirm or deny that my library will work for them. In particular I'd like to be compatible with OSX (which still ships with Bash 3.2, AFAIK).

I know Bash can run in POSIX-compliant mode; is there a similar setting to disable modern functionality? Or a way to run Bash in some sort of compatibility mode? I'm looking for any technique short of actually finding and booting up old operating systems and testing my library there.

Update

For example, I've avoided using associative arrays since they were introduced in Bash 4, but it's hard to be sure without testing that I'm not accidentally using some other Bash 4+ feature.


Solution

  • Although it's nice to know that it's possible to compile arbitrary versions of bash locally (as discussed in my other answer), these days there's a much simpler option - the official Docker bash images.

    To test a script against multiple bash versions is often as simple as:

    for v in 3 4 5; do # or whatever versions you're interested in
      docker run -v "$PWD:/mnt" "bash:$v" \
        bash /mnt/your_script.sh
    done