Search code examples
bashmacosshellzshzshrc

How can I get ZSH to pick up my latest version of Bash


My current shell is ZSH and it defaults to using Bash version 3.2.x. I'm on macos Sonoma version 14.3.1.

For one of the shell commands I'm trying to run, I'm currently getting the below error:

unbound variable and this requires using a version of bash >=4 for using a functionality with associative arrays.

I've installed the latest version of bash using brew and its currently available on this path:

/opt/homebrew/Cellar/bash/5.2.26/bin/bash

In my .zshrc I'm trying to add export PATH="$PATH:/opt/homebrew/Cellar/bash/5.2.26/bin" as a way to append the path to the latest version of bash that I want to use

But when I do bash --version I still get 3.2.57:

➜  ~ bash --version
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin23)
Copyright (C) 2007 Free Software Foundation, Inc.

How do I configure ZSH to load/use the latest bash version to solve the unbound variable error?

My /etc/shells looks like below(I updated it to add the latest bash version installed through brew):

/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
/opt/homebrew/bin/bash
/opt/homebrew/Cellar/bash/5.2.26/bin/bash

Solution

  • When you do

    export PATH="$PATH:/opt/homebrew/Cellar/bash/5.2.26/bin"
    

    you're making your new directory the LAST place to look for an instance of bash, you should have done

    export PATH="/opt/homebrew/Cellar/bash/5.2.26/bin:$PATH"
    

    to make it the first directory instead, otherwise some existing version of bash will be picked up by anything looking down your PATH.

    Also, make sure in your script you have a shebang of #!/usr/bin/env bash at the top of your script:

    #!/usr/bin/env bash
    echo "$BASH_VERSION" >&2     # just so you can see which version is used
    ...
    

    so it actually looks in your PATH for bash.