1

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

16
  • As an aside, I followed these instructions to install bash from source - gist.github.com/samnang/1759336 Commented May 30, 2019 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? Commented May 30, 2019 at 18:19
  • because that would change it for all shells - I am just looking to change for current shell if possible Commented May 30, 2019 at 18:24
  • Is bash not the current shell? If you mean current terminal than why doesn't an alias work? Commented May 30, 2019 at 18:28
  • aliases don't get inherited by subshells - youre right I meant all terminals - but all shells too I guess Commented May 30, 2019 at 18:30

1 Answer 1

3

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 } 
2
  • yeah but if it doesn't work with bash 3 then I might be able to get out of bash 3, will try it Commented May 30, 2019 at 19:15
  • so this works according to my testing, and I suppose it's better than appending stuff to the PATH? Commented May 30, 2019 at 19:42

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.