1

When running bash scripts inside zsh-Terminal I want it to use the homebrew bash version 4 instead of the default 3 of OS X.

How can I do that?

I installed bash 4 on my MacBook.

brew install bash

Instead of using it as the default shell using the following command, I want to keep my zsh.

chsh -s $(brew --prefix)/bin/bash # BAD! as I lose zsh

Still I want to run:

./my-cool-bash.sh
bfontaine
  • 18,169
  • 13
  • 73
  • 107
lony
  • 6,733
  • 11
  • 60
  • 92
  • `.sh` should only be used for POSIX-compatible scripts, in which case it shouldn't (technically) matter which (POSIX-compatible) shell you use to run the script; the results should be the same regardless. – chepner Jun 12 '17 at 16:21

4 Answers4

3

Use the following shebang in your scripts:

#!/bin/env bash

This makes them use the first bash in the PATH; which is the one you want.

This solution works with any Bash on any UNIX-like system.

bfontaine
  • 18,169
  • 13
  • 73
  • 107
2

use your new bash path: ex, if new bash is in /usr/local/bin/

/usr/local/bin/bash my-cool-bash.sh

or write first line of script:

#!/usr/local/bin/bash
tso
  • 4,732
  • 2
  • 22
  • 32
0

you could put an alias in your .zshrc file, something to the effect of alias mcb='./usr/local/bin/bash/my-cool-bash.sh so that you can call it from your normal zsh whenever you want.

Gleland
  • 270
  • 6
  • 14
0

In case you are solving this and you cannot change shebangs (because scripts are in shared codebase) or you want permanent solution, you can use following:

  • Open your configuration file for preferred shell. In my case .zshrc (in your case could be different)
  • And export path to your new bash.

So step by step:

  1. brew install bash (if you don't have homebrew, firstly install https://brew.sh/)
  2. which -a bash (outputs location of your bash installations)
  3. copy new bash location (in my case /opt/homebrew/bin/bash)
  4. nano ~/.zshrc (or open your shell configuration in different editor)
  5. add export PATH="/opt/homebrew/bin:$PATH" (the specific path differs based on value you got from step 2)
  6. Ctrl + X (to save in case of editor nano)
  7. Y to confirm (in case of editor nano)
  8. Restart your terminal or run source ~/.zshrc (to load changes)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Etaos
  • 640
  • 7
  • 8