“bash” command runs scripts in version 3.2, not 4.4

bashterminal

Currently, in terminal, my default interactive shell is bash version 4.4. The OS ships with 3.2.

If I want to run a script (e.g., my_script.sh) using bash version 4.4, I can source it (source my_script.sh) or type it into my terminal directly. Though, in each case the script runs in my current shell. I can also give a script execute permissions and run it as a command, allowing the shebang to control which version of bash to use.

However, the command bash continues to use version 3.2. For example, if I run bash my_script.sh, the script will be executed in a new shell (which I want), but the older version of bash will be used (3.2). Similarly, if I just run bash with no argument, a new shell opens up using version 3.2 (recall, if I open a new window or tab in terminal, it uses my default shell, bash v. 4.4. The problem here is what happens when I use the command bash).

I've added the path to bash 4.4 on my machine (/usr/local/bin/bash) to my PATH variable in .bash_profile, and it's not being overwritten somewhere else (echo $PATH gives the expected result: the very first path is usr/local/bin/bash). I expected this to change the behavior of the command bash

I can use a workaround, by setting an alias (alias bash4='/usr/local/bin/bash'), but I don't have to use an alias for bash 3.2, or for upgraded versions of, e.g., python or R.

Is there something I'm missing? Is the alias solution the only option here?

EDITS

in response to comments:

SHELL is /usr/local/bin/bash, which is unsurprising, since that's my default login shell.

type -a bash is interesting…

bash is /bin/bash
bash is /usr/local/bin/bash
bash is /bin/bash
bash is /usr/local/bin/bash
bash is /bin/bash
bash is /usr/local/bin/bash
bash is /bin/bash
bash is /usr/local/bin/bash

My entire PATH is a mess, which may be the source of this problem.

/usr/local/bin/bash:/Users/coltrane/Programming/Unix_Workbench/Code/Commands:/usr/local/bin/bash:/Users/coltrane/Programming/Unix_Workbench/Code/Commands:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/TeX/texbin:/opt/X11/bin:/usr/local/git/bin:/Applications/anaconda/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Users/coltrane/Programming/Unix_Workbench/Code/Commands:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/TeX/texbin:/opt/X11/bin:/usr/local/git/bin:/Applications/anaconda/bin:/Users/coltrane/Programming/Android_Development/sdk/platform-tools:/Library/Frameworks/R.framework/Resources/bin:/usr/local/git/bin:/Applications/anaconda/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Users/coltrane/Programming/Unix_Workbench/Code/Commands:/usr/local/bin/bash:/Users/coltrane/Programming/Unix_Workbench/Code/Commands:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/TeX/texbin:/opt/X11/bin:/usr/local/git/bin:/Applications/anaconda/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Users/coltrane/Programming/Unix_Workbench/Code/Commands:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/TeX/texbin:/opt/X11/bin:/usr/local/git/bin:/Applications/anaconda/bin:/Users/coltrane/Programming/Android_Development/sdk/platform-tools:/Library/Frameworks/R.framework/Resources/bin:/usr/local/git/bin:/Applications/anaconda/bin:/Users/coltrane/Programming/Android_Development/sdk/platform-tools:/Library/Frameworks/R.framework/Resources/bin

unrelated problem with my path, i had inadvertently repeated $PATH in one line of my .bash_profile, (PATH=$PATH::$PATH) causing unwanted duplication

Best Answer

The error here is that the first path in PATH was to the bash executable, instead of to the directory that contains the bash executable. For readers with similar problems: double check to make sure any path you're adding to your PATH variable resolves to a directory, not to a file.

Problem:

I had added the following to my .bash_profile:

PATH=/usr/local/bin/bash:$PATH

/usr/local/bin/bash is not a directory. It's a file (well, a symbolic link to a file). So, regardless of placing it at the very beginning of PATH, the command bash will not find an executable file named bash inside any directory in PATH until it gets to /bin.

Solution:

In .bash_profile, setting

PATH=/usr/local/bin:$PATH

Solves the problem. Now the command bash finds the executable inside /usr/local/bin before it gets to /bin.