1

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?

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

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

#!/usr/local/bin/bash

# the content of your function goes here

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

1
  • why didn't I think of that 🤦🏼‍♂️ thanks! – Can Sürmeli May 27 '20 at 13:55
1

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:

/usr/local/bin/bash
/bin/bash
2
  • 1
    The requirement from the OP was to specify a particular shell, not use the current shell du jour. – Allan May 29 '20 at 14:27
  • Thanks. This is actually quite insightful. – Can Sürmeli May 30 '20 at 13:54

You must log in to answer this question.

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