How can you export your .bashrc to .zshrc?

与世无争的帅哥 提交于 2019-12-20 08:04:10

问题


I am trying to move to zsh from Bash.

I put my .bashrc directly to my .zshrc, and it caused a lot of errors when I try to use Bash again.

How can you export your .bashrc to .zshrc?


回答1:


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:

.bashrc

source ~/.profile

.zshrc

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




回答2:


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.



来源:https://stackoverflow.com/questions/764600/how-can-you-export-your-bashrc-to-zshrc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!