How can you export your .bashrc to .zshrc?
Asked Answered
Z

5

128

I am trying to move to zsh from bash.

I copied my .bashrc directly to my .zshrc, and it caused a lot of errors when I tried to use bash again.

How can you export your .bashrc to .zshrc?

Zwart answered 19/4, 2009 at 1:7 Comment(0)
J
154

While lhunath's answer pushed me in the right direction, zsh does not seem to source .profile automatically. Lot's of good info on this topic can be found on this superuser post.

The adaption I'm using is putting common aliases and functions in .profile and manually sourcing them as follows:

In ~/.bashrc:

source ~/.profile

In ~/.zshrc:

[[ -e ~/.profile ]] && emulate sh -c 'source ~/.profile'

emulate is a zsh builtin command. With single argument set up zsh options to emulate the specified shell as much as possible.

Jeremiahjeremias answered 24/9, 2014 at 15:21 Comment(2)
the above answer is working fine for me also. but I have to do this every time I open the zshrc terminal. can you tell me how to correct this.Philippe
Have you written the code in the .zshrc file as specified or simply in the terminal? If you wrote it in the terminal, the fact that you have to do it each time is to be expected.Akimbo
N
42

You can't "export" your .bashrc to a .zshrc. .bashrc is a file that runs bash commands. .zshrc is a file that runs zsh commands.

You can't expect zsh to be able to run the bash commands in your .bashrc, so you should convert it into a new .zshrc instead of trying to run .bashrc from .zshrc or copying the former into the latter.

If you want a common shell initialization file for all your shells; use .profile (and remove .bashrc and .zshrc). It's sourced by all POSIX shells. And in there, stick to POSIX shell features only. Then that code will run in any POSIX shell. (Though, I'm not 100% certain that zsh is POSIX compliant).

See: http://mywiki.wooledge.org/DotFiles.

Though - and I'd first misread this part of your question - you shouldn't experience errors from bash when running your .bashrc unless you put zsh commands in there. Did you? What errors are you getting? Sounds to me like you've added zsh code into your .bashrc and bash (obviously) doesn't understand.

As an aside, ojblass tries to make a point of portability which only partly succeeds. zsh is a great shell (though I haven't had the honors myself), but when writing scripts; I'd recommend you do so with #!/usr/bin/env bash instead. Mostly just for your own (and eventually, the people you share with their) sake of portability.

Nonconductor answered 19/4, 2009 at 6:46 Comment(0)
Z
6

For me , the answer of Ryen came handy. But I made a slight change. I added all the aliases command in .profile in user directory ( vim ~/.profile).

alias gs='git status'
alias gp='git pull'
alias gph='git push'
alias gd='git diff | mate'
alias gau='git add --update'
alias gc='git commit -m'
alias gca='git commit -v -a'
alias gb='git branch'
alias gba='git branch -a'
alias gco='git checkout'
alias gcob='git checkout -b'
alias gcot='git checkout -t'
alias gcotb='git checkout --track -b'
alias glog='git log'
alias glogp='git log --pretty=format:"%h %s" --graph'
alias gfo='git fetch origin'

Then , I added source command in bash as well as zsh shell.

In bash shell ( vim ~/.bashrc)

source ~/.profile

In zsh shell ( vim ~/.zshrc )

source ~/.profile
Zither answered 5/2, 2021 at 16:28 Comment(1)
Just an fyi you can add all your git specific alias to your ~/.gitconfig instead. Then you can add fun stuff like publish = "!pub() { git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD);}; pub"Cabinda
O
5

For those who define their aliases in ~/.bash_aliases

The easiest way to centralize/use aliases is to reference them in ~/.zshrc

gedit ~/.zshrc :

...and add the following at the end:

...
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
    
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Once done then run:

source ~/.zshrc

And voilà... now you have shared aliases between bash and zsh.

Oram answered 6/6, 2022 at 9:46 Comment(0)
E
0

Add two lines code to your ~/.zshrc, zsh will autorun your customized commands in .bashrc.

# Exec ~/.bashrc and ~/.profile when using zsh
if [ -f '~/.profile' ]; then; source '~/.profile'; fi;
source <(awk '{ if(NR>118)print}' ~/.bashrc)  
# Line 118 is works for Ubuntu's default .bashrc

Note:

  • NR>118 works on Ubuntu, cause Ubuntu's default .bashrc has 118 lines and those lines should be ignored.
  • Your customized commands should append in end of .bashrc. Do not insert a new line in the system's bashrc area
Erasure answered 22/8, 2022 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.