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
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.
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
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.
Related
I've set a zsh alias like this:
alias sed="/usr/local/Cellar/gnu-sed/4.8/bin/gsed"
I can confirm it is working by running:
type sed
sed is an alias for /usr/local/Cellar/gnu-sed/4.8/bin/gsed
However, if I put exactly the same code, alias setting and type sed then in a script under the file name test and run it get the default sed:
zsh test
sed is /usr/bin/sed
I've also tried it with extending PATH and still get the same thing which puzzles me...
The only thing that worked for me was to create a function. After that sed was overriden with gsed when I called the help argument
#!/bin/bash
sed () {
/usr/local/Cellar/gnu-sed/4.8/bin/gsed "$#"
}
sed --help
If you want to use the command word sed as gsed in all your future scripts (which if you really want to do, you can do this), Homebrew has info on how to achieve this, at the end of the installation for brew install gsed:
GNU "sed" has been installed as "gsed".
If you need to use it as "sed", you can add a "gnubin" directory
to your PATH from your bashrc like:
PATH="/opt/homebrew/opt/gnu-sed/libexec/gnubin:$PATH"
Though you are asking about zsh so I'm assuming you've upgraded to at least macOS Catalina and are now trying to use zsh instead of bash, so that means I would suggest putting the suggested line from Homebrew into ~/.zshrc instead of ~/.bashrc.
Some things have changed, the default Homebrew installation directory is now, well you can use the command brew --prefix to get your directory, it's now currently /opt/homebrew instead of the old /usr/local/Cellar.
Now your sed should run as gsed in all your scripts.
Additional notes
Reasons why your code involving aliases isn't working as expected and some additional notes to take away (speaking to zsh and bash):
Aliases in a script file disappear after the script has been run (unless the script is called by another script, then the calling script has the new alias definition).
Aliases executed in startup files like "~/.zshrc" etc. aren't used within script files.
I am trying to install Anthos Service Mesh (ASM) for a Kubeflow installation and need /bin/bash to be v5+. MacOS comes with Bash v3.2.57 which doesn't work. Simply installing Bash v5+ in "/usr/local/bin" doesn't work either as several shell scripts for the install points to "/bin/bash" and thus I still get the old version.
I had hoped I could just temporarily move the new bash v5+ to "/bin/bash" and then revert after completing the ASM install - something like this:
>>>$sudo mv /bin/bash /bin/bash_old
>>>$sudo cp /usr/local/bin/bash /bin
>>>$make install_asm
>>>$sudo mv /bin/bash_old /bin/bash
>>>mv: rename /bin/bash to /bin/bash_old: Operation not permitted
So that doesn't seem to be possible
What would be the best way to get around this? It doesn't seem to work just adding an alias to .zshrc in the hope that whenever I execute a shellscript with "#!/bin/bash" it would actually call "/usr/local/bin/bash":
~/.zshrc:
alias /bin/bash="/usr/local/bin/bash"
>>>$/bin/bash --version
>>>GNU bash, version 5.1.8(1)-release (x86_64-apple-darwin19.6.0)
test_bash.sh:
#!/bin/bash
/bin/bash --version
>>>$sh ./test_bash.sh
>>>GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)
Perhaps there is a way for me be permitted to move the binaries as in the example above?
By the way already the "/usr/local/bin/bash" is a link - not sure if that has any influence on what I am trying to do.
>>>$ll /usr/local/bin/bash
>>>/usr/local/bin/bash -> ../Cellar/bash/5.1.8/bin/bash
Any hints are warmly welcomed!
I used a combination of adding my new shell location to the list of approved shells in /etc/shells, then changing my user's default shell with:
chsh -s /path/to/new/bash/version
as well as making sure my new bash location was exported to the front of my path not the end, so commands looking for just any bash find that first:
export PATH=/opt/homebrew/bin/bash:$PATH
No issues with this for far but this is a new machine and I'm just getting it set-up. If you have a SHELL environment variable set in any of your bash start-up scripts make sure to change it to your new bash binary also.
I've tried to upgrade my bash version on my Macbook Pro (Mojave OS). To do this, I've run:
brew install bash
sudo nano /etc/shells # And then added the new bash shell to the bottom of the list.
chsh -s /usr/local/Cellar/bash/5.0.11/bin/bash
After doing this, bash -version still returns version 3 but echo $BASH_VERSION print's version 5. If I try and make an associative array (I think this isn't present in 3), it works, so I assume I am using the new shell. Why has my bash version not updated?
Although you are running your updated version of bash, the command bash is (without a full path) still pointing to the original bundled version: /usr/bin/bash.
Assuming that you actually need to call the command in this form, then you should check the order of locations in $PATH, and make sure that the bin/ folder with the new bash command is in the $PATH list before /usr/bin. Failing that, make an alias in the shell pointing to the new bash.
brew adds the bash executable to /usr/local/bin/ directory.
I think this should work:
brew install bash
sudo nano /etc/shells # And then add /usr/local/bin/bash to the bottom of the list.
chsh -s /usr/local/bin/bash
I have homebrew's bash package installed. When I open a new shell:
bash --version gives GNU bash, version 5.0.7(1)-release (x86_64-apple-darwin18.5.0)
which bash gives /usr/local/bin/bash as expected.
But:
echo $BASH_VERSION yields 3.2.57(1)-release
I'm wondering if this is something I should address for scripts that might use this environment variable.
It means that the shell you're in is Bash 3.2, but bash points to Bash 5.0. Try bash and then, in the new shell, echo $BASH_VERSION – I guess it'll be 5.0. To change your login shell, add /usr/local/bin/bash to /etc/shells and change the default shell with
chsh -s /usr/local/bin/bash
sudo chsh -s /usr/local/bin/bash
After logging out and in again, $BASH_VERSION should be updated.
As for shebang lines, I recommend
#!/usr/bin/env bash
as it's portable and will use the first Bash in your $PATH.
The source of my problem was a terminal app preference setting. The "Command (complete path)" was set to /bin/bash. After setting it to "Default login shell", echo $BASH_VERSION reported the version I expected. The other problem is I stupidly ran the bash --version command in iTerm2, not terminal. So it gave a different response than what terminal would have.
Your login shell (see echo $SHELL) is probably /bin/bash and that is the one setting $BASH_VERSION. If you need to use a specific version in scripts, use the full path in the #! line.
Make sure to check that the terminal you are using is using the default login shell and not a custom one.
Hi I've recently installed zsh using cygwin on my Windows machince but when I type zsh to start this I get the following:
GG#GG-PC ~
$ zsh
\[\e]0;\w\a\]\n\[\e[32m\]\u#\h \[\e[33m\]\w\[\e[0m\]\n\$
On my mac I am using iTerm2 and this is so much easier to setup on here. Also I am having trouble in setting up the aliases and this is becauses its not setup properly in terms of config file where I can set this up in a separate file.
Any ideas how I can resolve?
It looks like zsh is inheriting the value of PS1 from the previous shell. The PS1 environment variable sets the shell prompt, and zsh used a different format for prompt substitutions than other shells. Try entering the following command after you start zsh:
PS1=$'%{\e]0;%d\a%}\n%F{green}%n#%m %F{yellow}%d%f\n%# '
If that works, add that line to your ~/.zshrc file.
That's also probably a good place to put your aliases.
There might be an issue because you launch zsh from bash actually and not cygwin.
One thing you can do is to launch zsh as the starting shell of mintty (the window that wraps your shell)
Create a shortcut with this inside:
c:\<cygwin-folder>\bin\mintty.exe -i /Cygwin-Terminal.ico /usr/bin/zsh --login -
Yo need to update .zshrc with your required theme and then
source .zshrc