Upgrade bash to 4.0 in Mac OS X

bash 4.0 was released last month, and I'm only now getting around to messing with it. So far the things I use most are the '**' recursive globbing and the 'autocd' option. Here's how I upgraded my system and a few ways I use the new features.

  1. Download, build and install. Get the source tarball:
    $ wget ftp://ftp.cwru.edu/pub/bash/bash-4.0.tar.gz

    Build and install:
    $ tar xzf bash-4.0.tar.gz
    $ cd bash-4.0
    $ ./configure && make && sudo make install

    On my system (Leopard + Xcode) I had no trouble compiling. The binary is installed at /usr/local/bin/bash. You can also install via MacPorts or Fink, I imagine, but I try to do these things myself when possible, if only for the sake of transparency.

  2. Configure to use the new shell. First, register the new binary as a valid shell:
    $ sudo bash -c "echo /usr/local/bin/bash >> /private/etc/shells"

    Then change your user to use it as its shell with chsh (this is blatantly obvious, but modify the "Shell" line to point to /usr/local/bin/bash).
    Now open a new shell (restarting Terminal.app will do, or just Cmd-N for a new window) and make sure your changes took:
    [ian@iansmbp] ~/> echo $SHELL
    /usr/local/bin/bash
    [ian@iansmbp] ~/> echo $BASH_VERSION
    4.0.0(1)-release

  3. Enable the new features. There's a bunch of features in the 4.0 release that I haven't gone through yet, including coproc, improved programmable completion, a new &>> redirect operator (synonym of the old >>myfile 2>&1 pattern) and new case-insensitive expansion options. You can peruse a list of the new stuff at your leisure. For immediate gratification, however, just turn on recursive globbing and autocd:
    $ echo "shopt -s globstar autocd" >> ~/.profile
    $ source ~/.profile

    ** matches contents recursively. For example, where you might previously have recursively removed all your byte-compiled Python modules with:
    $ find . -name \*.pyc | xargs rm -f

    You can now simply:
    $ rm -f **/*.pyc

    If you've got a deep directory structure, and you want to get into a subdirectory whose name you know, you can replace:
    $ cd path/to/the/directory/i/want/named/mydir

    with:
    $ cd **/mydir

    Hey, and if those extra three characters at the beginning of that last command are too much for you, then you'll love the autocd option, which, when enabled, permits you to cd to a directory merely by typing its name:
    [ian@iansmbp] ~/src/zenoss/core/Products/> **/yui
    cd ZenWidgets/skins/zenui/yui
    [ian@iansmbp] ~/src/zenoss/core/Products/ZenWidgets/skins/zenui/yui/>


More on new features as warranted; these two, however, are those of zsh that I missed most.

10 comments

  • peter  
    July 8, 2009 at 9:45 AM

    echo $BASH_VERSION
    4.0.0(1)-release

    but:

    bash --version
    GNU bash, version 2.05b.0(1)-release (powerpc-apple-darwin8.0)

    how can it be? I have done everything as you.
    thanks for the help.

  • Anonymous  
    July 23, 2009 at 3:36 AM

    echo $BASH_VERSION
    4.0.0(1)-release
    but:
    bash --version
    GNU bash, version 2.05b.0(1)

    How can it be? I did everything exactly as you.

    thanks,
    peter

  • Anonymous  
    April 5, 2010 at 3:48 AM

    the install is failing at make:

    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    make: *** [bash] Error 1


    can you help?

  • Anonymous  
    December 3, 2010 at 1:27 AM

    +1, this works very well.

  • Ethan Cane  
    December 11, 2010 at 5:48 PM

    You will need to shift around the paths found in /etc/paths in order to place /usr/local/bin ahead of /bin so that when executing bash --version it will find your newly installed shell.

    I cannot see how this would adversely affect system stability since very few executables should be located within /usr/local/bin which take the place of system level binaries.

  • Anonymous  
    October 10, 2012 at 1:38 PM

    Thanks a lot.
    It works on my iOS 10.6.8.

  • Anonymous  
    November 19, 2012 at 2:50 PM

    nice - thanks! had been kinda-sorta remembering autocd capability from tcsh years ago.

  • Anonymous  
    February 28, 2013 at 11:28 AM

    This worked great, thanks for the nice write-up.

    For anybody else searching for the same thing, some keywords... Upgrading Bash let me install RVM on an old PowerBook G4 I'm resuscitating. RVM's installer doesn't work on Leopard otherwise—it wants at least Bash 3.2.25, but Leopard (10.5.8) ships with Bash 3.2.17.

    p.s. Bash 4.2 is out now, so I used that instead. The 4.2 source is in the same directory on that FTP server; just update "bash-4.0" to "bash-4.2" throughout the instructions.

  • Anonymous  
    December 16, 2015 at 10:04 AM

    and then there's this: http://tenfourfox.blogspot.ca/2014/09/bashing-bash-one-more-time-updated.html
    a custom built bash 4.3

  • Valerie  
    July 23, 2024 at 4:54 AM

    Thiss is a great blog

Post a Comment