bash 如何将 .bashrc 导出到 .zshrc?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/764600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:07:08  来源:igfitidea点击:

How can you export your .bashrc to .zshrc?

zshbashzshrc

提问by Léo Léopold Hertz ??

I am trying to move to zsh from Bash.

我正在尝试从 Bash 转移到 zsh。

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

我把我的 .bashrc 直接放到了我的 .zshrc 中,当我再次尝试使用 Bash 时,它导致了很多错误。

How can you export your .bashrc to .zshrc?

如何将 .bashrc 导出到 .zshrc?

回答by Ryne Everett

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

虽然 lhunath 的回答将我推向了正确的方向,但 zsh 似乎并没有.profile自动来源。在这个超级用户帖子中可以找到很多关于这个主题的好信息。

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

我正在使用的适应是将常见的别名和函数放入.profile并手动获取它们,如下所示:

In ~/.bashrc:

~/.bashrc

source ~/.profile

In ~/.zshrc:

~/.zshrc

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

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

emulate是一个 zsh 内置命令。使用单个参数设置 zsh 选项以尽可能地模拟指定的 shell。

回答by lhunath

You can't "export" your .bashrcto a .zshrc. .bashrcis a file that runs bashcommands. .zshrcis a file that runs zshcommands.

您不能“导出.bashrc.zshrc. .bashrc是一个运行bash命令的文件。 .zshrc是一个运行zsh命令的文件。

You can't expect zshto be able to run the bashcommands in your .bashrc, so you should convert it into a new .zshrcinstead of trying to run .bashrcfrom .zshrcor copying the former into the latter.

你不能指望zsh能够运行bash在你的命令.bashrc,所以你应该把它转换成一个新的.zshrc,而不是试图运行.bashrc.zshrc或复制前者到后者。

If you want a common shell initialization file for all your shells; use .profile(and remove .bashrcand .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 zshis POSIX compliant).

如果您想要所有 shell 的通用 shell 初始化文件;使用.profile(并删除.bashrc.zshrc)。它来自所有 POSIX shell。在那里,坚持 POSIX shell 功能。然后该代码将在任何 POSIX shell 中运行。(不过,我不能 100% 确定它zsh符合 POSIX 标准)。

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

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

Though - and I'd first misread this part of your question - you shouldn't experience errors from bashwhen running your .bashrcunless you put zshcommands in there. Did you? What errors are you getting? Sounds to me like you've added zshcode into your .bashrcand bash(obviously) doesn't understand.

虽然 - 我首先误读了您问题的这一部分 -除非您在其中放置命令,否则您bash在运行时不应该遇到错误。你是否?你有什么错误?在我看来,您已将代码添加到您的并且(显然)不理解。.bashrczshzsh.bashrcbash

As an aside, ojblasstries to make a point of portability which only partly succeeds. zshis 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 bashinstead. Mostly just for your own (and eventually, the people you share with their) sake of portability.

ojblass顺便说一句,试图提出仅部分成功的可移植性点。 zsh是一个很棒的外壳(虽然我自己没有获得荣誉),但是在编写脚本时;我建议你这样做#!/usr/bin/env bash。主要是为了您自己(以及最终与您共享的人)的便携性。