PRIMARY CATEGORY → BASH
RESOURCES
BASH VersioningSee here

If a Shell Script is not POSIX Compliant and a specific Non-POSIX Shell Functionality is being used (e.g. local in Bash), It may arises some errors and unintended actions if that script is interpreted by a POSIX Compliant shell like sh or dash

The same applies if the script implement some functionality that is specific of the shell for which it is being created

Therefore, It’s necessary to perform some checks at the beginning of the script to prevent above problems

Shell Check

POSIX Compliant

foo()
{
        case $( /bin/ps -p "$PPID" -o comm= ) in
 
                *bash)  return 0
                        ;;
 
                *)      printf "Shell not allowed. Exiting...\n" 1>&2
                        return 1
                        ;;
        esac
}
Bash Version Check

It can be done through BASH_VERSION escalar parameter and BASH_VERSINFO indexed array

  • BASH_VERSION
$ declare -p -- BASH_VERSION
declare -- BASH_VERSION="5.1.16(1)-release"
  • BASH_VERSINFO
$ printf "=%s= " "${BASH_VERSINFO[@]}"
=5= =1= =16= =1= =release= =x86_64-pc-linux-gnu=
vX.0

e.g. Bash v4.0

bar()
{
      (( BASH_VERSINFO[0] < 4 )) && {
              printf "Bash Version < v4.0. Exiting...\n" 1>&2
              return 1
      }
 
      return 0
}
vX.X

e.g. Bash v4.4

bar()
{
        (( BASH_VERSINFO[0] < 4 || ( BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] < 4 ) )) && {
 
                printf "Bash Version < v4.4. Exiting...\n" 1>&2
                return 1
        }
}