I have installed bash 4 with Homebred and bash --version shows me the version 4.

If I have following test script:

# test-version.sh

And I make it executable chmod +x configurer.sh and run it ./test-version.sh I also get the version 4.

But if I run it with the sh command sh test-version.sh I get the version 3.

I have put the new shell into /etc/shells:

sudo bash -c 'echo /usr/local/bin/bash >> /etc/shells'

And changed to the new shell:

 chsh -s /usr/local/bin/bash

I also changed the path in iTerm Settings > Profiles > Command to /usr/local/bin/bash.


EDIT: I've come to realise that it's good to leave sh alone. When wanting to run scripts with Bash 4 just don't use sh.

  • Can you please tell us the out put of echo $PATH? – abc Oct 25 '18 at 16:58
  • When you run ‘sh command’ you are starting a new shell with its own environment and path. Is there a reason you want to do this? Why not start your script with the shevang and /usr/env bash line so that it picks up the gash shell as you expect? – Allan Oct 25 '18 at 18:48

I used homebrew to install BASH4 and I use the shebang #!/bin/bash every time. This works for me with both sh and ./ styles of running.

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

  • Have you linked that to /usr/local/bin/bash? For me that shebang gives me Bash 3 always. – any_h Oct 26 '18 at 4:54

A simple fix if you just want to be able to write "sh" and have it run the new bash is to create a link like this:

ln -s /usr/local/bin/bash /usr/local/bin/sh

This won't help you if you have for example scripts that start with #!/bin/sh or similar.

In order to change that sh, you'll need to either copy over the /usr/local/bin/bash file to overwrite /bin/sh, like this:

  cp /usr/local/bin/bash /bin/sh

or create a link between the two:

  ln -sf /usr/local/bin/Bash /bin/sh

Please take a backup copy of the sh binary before doing so.

However, I would advice to leave "sh" alone as it is the system shell used during bootup, shutdown, and by the system in general. Even though the newer bash version is largely compatible, it would be tiresome if booting fails now or after an OS upgrade because you have substituted the shell.

  • @abc There's no point in changing the PATH variable. /usr/local/bin and /usr/bin already has higher preference than /bin. I already wrote in the answer, that it is not advisable to replace the sh file in general. – jksoegaard Oct 25 '18 at 17:08
  • Oh, I didn't see that. You should use links instead of copying the file over, and rename the old sh to something else so you can always recover it if needed. – abc Oct 25 '18 at 17:12
  • For making links, I think it's usually better to use symlinks than hard links. – abc Oct 25 '18 at 17:13
  • @abc The answer is already with symlinks - there’s no hard links at all.... – jksoegaard Oct 25 '18 at 17:21
  • Symlinks are created with ln -s. Hard links are created with plain ln. See here.(I've edited the answer to use symlinks) – abc Oct 25 '18 at 17:47

You must log in to answer this question.

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