In my current terminal setup, I make use of zsh; it's my default shell. I also have an up-to-date version of Bash for other reasons installed from Homebrew.

However, there is also the Bash provided by Apple which is at version 3.2.57.

I've my own functions setup and a particular one requires a feature starting from Bash 4.0. Even though my default shell is zsh and I aliased sh(Apple's bash) command to bash(the bash from Homebrew) command, the function I've defined still defaults to using sh and hence, my function doesn't/can't function. I've also changed the shebang at the head of the file defining my custom function but no help.

How can I make that function I've defined default to using the bash installed from Homebrew?

  • If you create a subshell to run your function, whatever happens in that subshell is contained only in that subshell.
    – Allan
    May 27, 2020 at 12:46

2 Answers 2


You can turn the function into a shell script and define the shell to be used in the first line


# the content of your function goes here

Then make the shell script executable and put it somewhere into your PATH.


The other approach will work, but would fail if /usr/local/bin/bash does not exist.

If you want to ensure future compatibility, I'd recommend you specify the shebang line using /usr/bin/env, which ensures your environment uses the path to find the first version of the binary (bash).

#!/usr/bin/env bash

# the content of your script

If you have configured your environment to search /user/local/bin first, this will function as you expect.

You can check where bash exists in your path with:

$ which -a bash

Which outputs the following on my system:

  • 1
    The requirement from the OP was to specify a particular shell, not use the current shell du jour.
    – Allan
    May 29, 2020 at 14:27
  • Thanks. This is actually quite insightful. May 30, 2020 at 13:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .