I'M using associative arrays in my script, which are available in bash version 4 and upwards. In terms of best practices, should I verify the version is at least 4? E.g. using echo $BASH_VERSION | cut -d. -f1.

Although bash 4 was released in early 2009, some OSes are still using older versions. E.g. the default bash on OS X is stuck on bash 3.2 (for legal reasons).

  • 3
    Simpler: ${BASH_VERSION%%.*} – jasonwryan Dec 21 '15 at 19:45
  • You don't have to modify it at all; just pattern match: if [[ $BASH_VERSION == 4.* ]]; – chepner Dec 21 '15 at 20:16
  • 5
    @chepner I actually like (( ${BASH_VERSION%%.*} >= 4 )), it's future proof for versions 5+ – dimid Dec 21 '15 at 20:20
up vote 2 down vote accepted

Yes. Doing extra sanity checking is rarely a bad thing especially if there are reasonable situations where the check would fail and thereby prevent unexpected behavior.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.