How to check Bash script on older versions of Bash?

I am working on a Bash library and want me to support as many environments as possible, including older Bash installations. My development environment is Bash 4.3, but some of my users may well work with much older versions, and currently I have no way to confirm or deny that my library will work on them. In particular, I would like to be compatible with OSX (which still comes with Bash 3.2, AFAIK).

I know that Bash can work in POSIX-compatible mode; Is there a similar setting to disable modern features? Or a way to run Bash in some kind of compatibility mode? I am looking for any technique that does not allow finding and loading old operating systems and testing my library.

Refresh

For example, I avoided using associative arrays since they were introduced in Bash 4, but it's hard to be sure without checking that I don't accidentally use any other Bash 4+ function.

+6
source share
3 answers

Finally, returning to this question, it is quite easy to simply compile (without installing) the bash version (s) . Interestingly, this is how I test Bash 3.2.57:

$ mkdir ~/bash $ cd ~/bash $ wget http://ftp.gnu.org/gnu/bash/bash-3.2.57.tar.gz $ tar xvzf bash-3.2.57.tar.gz $ cd bash-3.2.57 $ ./configure $ make # if `make` fails due to yacc, run `sudo apt-get install byacc` # No need to run `make install` $ ./bash -version GNU bash, version 3.2.57(1)-release (armv7l-unknown-linux-gnu) Copyright (C) 2007 Free Software Foundation, Inc. 

You now have a Bash 3.2.57 binary that you can run without actually “installing” or changing your normal environment.

To run a shell script for this version:

 $ ./bash your_script.sh 

To enter a blank interactive prompt :

 $ env -i PATH="$PWD:$PATH" ./bash --noprofile --norc bash-3.2$ bash -version GNU bash, version 3.2.57(1)-release (armv7l-unknown-linux-gnu) Copyright (C) 2007 Free Software Foundation, Inc. bash-3.2$ 

Using env -i , and not just calling ./bash , will lead you to a usually empty environment (run env from within the shell to see what is still set). Updating PATH allows you to invoke bash (e.g. bash -version ) to invoke the local Bash shell, rather than a system-wide installation (but note that this pulls in your entire PATH). Adding --noprofile --norc avoids loading your .bashrc and related scripts.

If you don't want to select any PATH modifications, just do export PATH="$PWD:$PATH" once inside the subshell, and not as part of the env command.


These installation steps are now part of the Docker image , if it is useful to people. I do not necessarily suggest using this image directly, but you can copy it from the Dockerfile. MIT License.

+5
source

Take a look at shenv: https://github.com/shenv/shenv . Like rbenv, pyenv, goenv and others, but for shells it allows you to install other versions of Bash among others (zsh, fish, yash, etc.).

(Disclaimer: I am the one who bisected the Pienws to Shen!)

0
source

You can use your own Bash tools to emulate the old version of Bash. Check out the " shopt " options on shopt .

Keep in mind that if you change the behavior described in each compatNN on the manual page, it does not remove functions that are otherwise present in the current version. For example, this does not cause errors:

 shopt -s compat31 shopt -s globstar 

Although globstar was introduced only on Bash 4.0.

0
source

Source: https://habr.com/ru/post/1240665/


All Articles