0

I am trying to setup a system to switch between bash versions on a Mac. I have this which works:

change_bash_version(){

  if test "$1" == "3"; then

    export PATH="$HOME/bash/versions/3.2.57:${PATH}"

  elif test "$1" == "4"; then

    export PATH="$HOME/bash/versions/4.4.18:${PATH}"

  else

    export PATH="$HOME/bash/versions/5.0:${PATH}"

  fi

}

export -f change_bash_version

I tried using aliases but they are not inherited - my question is - is there an alternative to changing the PATH to putting the desired bash executable in front? It doesn't work very easily to put an entire directory on the PATH if that dir has a bunch of extraneous files that I don't want the PATH to see.

The other downside to this technique is having to use a bash function to switch versions (unless someone can think of way to use a command line utility).

I tried using this:

change_bash_version(){

  bash(){
    $HOME/bash/versions/4.4.18/bash "$@"
  }

  export -f bash;  # error here
}

but bash did not like that - saying "bash" is not a function

  • As an aside, I followed these instructions to install bash from source - gist.github.com/samnang/1759336 – Alexander Mills May 30 at 18:13
  • Why not use your /usr/local/bin as the first directory in your PATH and have your function create/change a symlink in there to the bash version you desire? – Jesse_b May 30 at 18:19
  • because that would change it for all shells - I am just looking to change for current shell if possible – Alexander Mills May 30 at 18:24
  • Is bash not the current shell? If you mean current terminal than why doesn't an alias work? – Jesse_b May 30 at 18:28
  • aliases don't get inherited by subshells - youre right I meant all terminals - but all shells too I guess – Alexander Mills May 30 at 18:30
2

This seems to work for me with very basic testing:

change_bash_version(){
    case $1 in
        3) bash() { "$HOME/bash/versions/3.2.57/bash" "$@"; } ;;
        4) bash() { "$HOME/bash/versions/4.4.18/bash" "$@"; } ;;
        *) bash() { "$HOME/bash/versions/5.0/bash"    "$@"; } ;;
    esac
    export -f bash
}
  • yeah but if it doesn't work with bash 3 then I might be able to get out of bash 3, will try it – Alexander Mills May 30 at 19:15
  • so this works according to my testing, and I suppose it's better than appending stuff to the PATH? – Alexander Mills May 30 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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