How to Test My Bash Script on Older Versions of Bash

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

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

Old version of script is run unless invoked with sh scriptname

Use a full or relative path to the script to make sure you're running what you think you're running.

If you are running it as simply script.sh then the shell will PATH environment variable lookup to locate it. To see which script.sh bash would be using in that case, run type script.sh.

Relative Path

./script.sh

Full Path

/path/to/my/script.sh

Regex pattern matching in old versions of bash

Try

if ! echo "$FIRST_LINE" | grep -Eqe "$PATTERN"; then

or (if your shell supports here-strings <<<)

if ! grep -Eqe "$PATTERN" <<< "$FIRST_LINE"; then

PS: By convention all-capital variable names are reserved for environment and shell variables. Use lowercase variables like $pattern and $firstLine to avoid accidental name collisions.

PPS: Your regex could be slightly simplified. [v] is the same as v and so on.

^[[:alpha:]]{2,6}[-][[:digit:]]{0,9}-[-][v][[:digit:]][.][[:digit:]]
^[[:alpha:]]{2,6}-[[:digit:]]{0,9}--v[[:digit:]]\.[[:digit:]]

Check if Bash script is compatible with sh

Being sh-compatible isn't, in itself, a goal. What issue(s) are you running into that requires your script work with sh? Depending on your reasoning different options may or may not be sufficient. If you simply need your script to run on most modern environments then using bash should be perfectly fine, and may actually be better than using sh, since sh maps to different shells on different platforms. On most modern environments it isn't actually its own binary but is just bash in POSIX-compliant mode, or another shell like dash.

If you really just need to make an existing Bash script work with the shebang #!/bin/sh you have several options:

  1. Just run it as sh your_script.sh and see what happens - Bash is a superset of sh syntax, so many simple Bash scripts are valid sh scripts.
  2. Run sh -n your_script.sh as rojomoke suggests, which will report syntax errors without actually executing the script (but may not catch all issues).
  3. Manually audit your script for invalid syntax (John B's Bashisms reference isn't a bad start), this obviously isn't a great solution but in practice it's the only way to be sure. If that seems daunting, well, it is :)

If you want/need to support sh the best option is simply to specify your script's shebang as #!/bin/sh - if it behaves as desired in the environments you need it to then it's (by definition) sh-compatible.

Note that you can write a sh-compatible script that still isn't POSIX-compliant, since many standard utilities like grep have their own POSIX-compliant feature sets you'd need to respect too. Again though, being POSIX-compliant isn't an end in itself, and I'd encourage you to confirm you really need to support POSIX before trying to be compliant with the standard.

I asked a related question you might find helpful too.

bash loop: for loop in older bash versions

That was added in bash 4, I believe.

You can do:

for (( k=5; k<=201; k+=2 )); do
filename=$filename$k
done


Related Topics



Leave a reply



Submit