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.
Related
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.
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.
I want to restart vim from a bash script so that vim picks up out-of-band changes. I almost have it working but I am stuck trying to determine what to use to launch vim.
Here's what I have:
#!/usr/bin/env bash
local servername=$(vim --serverlist)
[ -n "$servername" ] && {
vim --servername "$servername" --remote-send '<C-\><C-N>:mks! _session.vim<CR>:wqa<CR>'
vim -S _session.vim
sleep 1
rm _session.vim
}
The problem is the vim called by the script is the very obsolete system vim at /usr/bin/vim, not "my" vim which is an alias to mvim -v (where mvim is the launch script which comes with MacVim).
This has two unfortunate consequences: (1) the system vim doesn't recognise --serverlist; (2) even if it did my script would subsequently launch the wrong vim.
What's the best way to invoke the vim on my path?
The default vim is never built with +clientserver so the portability you are afraid to loose was never there to begin with.
Aliases are not expanded in bash scripts so your script won't see mvim -v if you don't tell it explicitly to use that. Furthermore, your vim is an alias so it is not in your PATH.
You could define an environment variable somewhere near the top of your script and use it instead of vim:
#!/usr/bin/env bash
VIM='/path/to/mvim'
"$VIM" -v whatever
Or turn your alias into a proper script.
Or, maybe, place mvim earlier in your PATH and call mvim -v explicitly.
I'm trying to run source /Users/alastair/.rvm/scripts/rvm, but keep getting:
Bad : modifier in $ (").
Where would the problem be? Happy to paste other files in if these would help.
Are you in any case running a shell that is not Bash or ZSH? Bash >= 3.2.25 or ZSH >= 4.3.10 is required.
Your problem looks like you were using minimalistic shell sh which is not supported by RVM.
You can check user shell in /etc/passwd and change it with chsh -s /path/to/new/shell - list of allowed shells is available in /etc/shells - but make sure to pick Bash/ZSH, also note that links like sh->bash will not work as bash changes behavior based on the name that was invoked.
mvim is installed in /usr/local/bin/ but can not be used as either EDITOR or VISUAL:
$ mvim -f # works as expected
$ EDITOR="/usr/local/bin/mvim -f" crontab -e
crontab: /usr/local/bin/mvim -f: No such file or directory
crontab: "/usr/local/bin/mvim -f" exited with status 1
I tried single quotes and using VISUAL instead of EDITOR. Same result. I also tried googling, but apparently the -f flag works just fine for everybody else.
I use Mac OS 10.6.6 and zsh, but the problem is the same in bash.
The problem is crontab expects to be able to run a program called "/usr/local/bin/mvim -f" if you supply that in the EDITOR environment variable.
To get around that, you could write a short shell script. For example, call this one mvimf:
#!/bin/bash
/usr/local/bin/mvim -f "$#"
Then you can run: EDITOR=/usr/local/bin/mvimf crontab -e
I am not sure if this is directly related to the problem you are having but I was seeing a similar error code when trying to edit my crontab. I realized I had a little conflict in my vimrc file related to the pathogen plugin. If you call:
filetype off
when it's already off you can cause problems that will make your Vim exit with errors. Sounds like your issue is fixed already, but since this shows up in searches related to this error code, I thought I would post it here.
Credit goes to commenters on this post - http://tooky.github.com/2010/04/08/there-was-a-problem-with-the-editor-vi-git-on-mac-os-x.html
For those seeing this without mvim, you can use morton-fox's answer for any editor:
EDITOR=/usr/bin/vim crontab -e
Will use vim to open the crontab file