0

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

If I have following test script:

#!/usr/local/bin/bash
# test-version.sh
echo $BASH_VERSION

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.

Thanks!

EDIT: I've come to realize 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
1

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.

| improve this answer | |
  • 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
-1

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.

| improve this answer | |
  • @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
-1

By running man sh, we can learn the sh command determines the shell first by evaluating a symbolic link:

DESCRIPTION
     sh is a POSIX-compliant command interpreter (shell).  It is implemented by re-execing as either bash(1), dash(1), or zsh(1) as determined by the sym-
     bolic link located at /private/var/select/sh.  If /private/var/select/sh does not exist or does not point to a valid shell, sh will use one of the
     supported shells.

Verifying this as follows:

$ l /private/var/select
drwxr-xr-x - root 13 Oct  2019  .
drwxr-xr-x - root 26 May 21:25  ..
lrwxr-xr-x 9 root 13 Oct  2019  sh -> /bin/bash

If you wish to alter the default shell, you can update this symbolic link accordingly.

In addition, I would verify that whichever shell you use is sanctioned in your /etc/shells file:

$ cat /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
/usr/local/bin/zsh
| improve this answer | |
  • 1
    Welcome to AskDifferent. Please be aware that macOS is not Linux and sh actually means the binary /bin/sh (the man page you quote is definitively not the macOS version either). – nohillside 8 hours ago

You must log in to answer this question.

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