如何将 .bashrc 导出到 .zshrc?
我正在尝试从 bash 迁移到 zsh。
我将 .bashrc 直接复制到 .zshrc 中,当我再次尝试使用 bash 时,它导致了很多错误。
如何将 .bashrc 导出到 .zshrc?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
虽然 lhunath 的回答将我推向了正确的方向,但 zsh 似乎并没有自动获取
.profile
源。 有关此主题的大量有用信息可以在这篇超级用户帖子中找到。我正在使用的调整是将通用别名和函数放入
.profile
中,并按如下方式手动获取它们:在
~/.bashrc
中:在
~/.zshrc 中
:模拟
是 zsh 内置命令。 使用单个参数设置 zsh 选项来尽可能模拟指定的 shell。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
:In
~/.zshrc
:emulate
is a zsh builtin command. With single argument set up zsh options to emulate the specified shell as much as possible.您无法将
.bashrc
“导出”到.zshrc
。.bashrc
是运行bash
命令的文件。.zshrc
是运行zsh
命令的文件。您不能期望
zsh
能够运行.bashrc
中的bash
命令,因此您应该将其转换为新的>.zshrc
而不是尝试从.zshrc
运行.bashrc
或将前者复制到后者中。如果您想要所有 shell 都有一个通用的 shell 初始化文件; 使用
.profile
(并删除.bashrc
和.zshrc
)。 它源自所有 POSIX shell。 在那里,仅坚持 POSIX shell 功能。 然后该代码将在任何 POSIX shell 中运行。 (不过,我不能 100% 确定zsh
是否符合 POSIX)。请参阅:http://mywiki.wooledge.org/DotFiles。
不过 - 我首先会误读你问题的这一部分 - 在运行
.bashrc
时,你不应该遇到来自bash
的错误,除非你输入zsh命令在那里。 你是否? 您遇到什么错误? 在我看来,您已经将
zsh
代码添加到.bashrc
中,而bash
(显然)不理解。顺便说一句,
ojblass
试图强调可移植性,但只取得了部分成功。zsh
是一个很棒的 shell(尽管我自己没有获得过这种荣誉),但是在编写脚本时; 我建议您使用#!/usr/bin/env bash
来执行此操作。 主要只是为了您自己(以及最终与您共享的人)的可移植性。You can't "export" your
.bashrc
to a.zshrc
..bashrc
is a file that runsbash
commands..zshrc
is a file that runszsh
commands.You can't expect
zsh
to be able to run thebash
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 thatzsh
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 putzsh
commands in there. Did you? What errors are you getting? Sounds to me like you've addedzsh
code into your.bashrc
andbash
(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.对我来说,Ryen 的回答很方便。 但我做了一点小小的改变。 我在用户目录( vim ~/.profile )的 .profile 中添加了所有别名命令。
然后,我在 bash 和 zsh shell 中添加了 source 命令。
在 bash shell 中 ( vim ~/.bashrc )
在 zsh shell 中 ( vim ~/.zshrc )
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).
Then , I added source command in bash as well as zsh shell.
In bash shell ( vim ~/.bashrc)
In zsh shell ( vim ~/.zshrc )
对于那些在 ~/.bash_aliases 中定义别名的人来说,
集中/使用别名的最简单方法是在 ~/.zshrc 中引用它们
gedit ~/.zshrc :
...并在end:
一旦完成然后运行:
瞧...现在你已经在 bash 和 zsh 之间共享了别名。
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:
Once done then run:
And voilà... now you have shared aliases between bash and zsh.
添加两行代码到您的
~/.zshrc
中,zsh将自动运行您在.bashrc
中的自定义命令。注意:
NR>118
适用于 Ubuntu,因为 Ubuntu 默认的.bashrc
有 118 行,这些行应该被忽略。.bashrc
的末尾。 不要在系统的 bashrc 区域插入新行Add two lines code to your
~/.zshrc
, zsh will autorun your customized commands in.bashrc
.Note:
NR>118
works on Ubuntu, cause Ubuntu's default.bashrc
has 118 lines and those lines should be ignored..bashrc
. Do not insert a new line in the system's bashrc area