如何将 .bashrc 导出到 .zshrc?

发布于 2024-07-17 03:08:06 字数 138 浏览 3 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

迷荒 2024-07-24 03:08:06

虽然 lhunath 的回答将我推向了正确的方向,但 zsh 似乎并没有自动获取 .profile 源。 有关此主题的大量有用信息可以在这篇超级用户帖子中找到。

我正在使用的调整是将通用别名和函数放入 .profile 中,并按如下方式手动获取它们:

~/.bashrc 中:

source ~/.profile

~/.zshrc 中

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

模拟是 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:

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.

才能让你更想念 2024-07-24 03:08:06

您无法将 .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 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.

萌酱 2024-07-24 03:08:06

对我来说,Ryen 的回答很方便。 但我做了一点小小的改变。 我在用户目录( vim ~/.profile )的 .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'

然后,我在 bash 和 zsh shell 中添加了 source 命令。

在 bash shell 中 ( vim ~/.bashrc )

source ~/.profile

在 zsh shell 中 ( vim ~/.zshrc )

source ~/.profile

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
半暖夏伤 2024-07-24 03:08:06

对于那些在 ~/.bash_aliases 中定义别名的人来说,

集中/使用别名的最简单方法是在 ~/.zshrc 中引用它们

gedit ~/.zshrc :

...并在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

一旦完成然后运行:

source ~/.zshrc

瞧...现在你已经在 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:

...
# 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.

一城柳絮吹成雪 2024-07-24 03:08:06

添加两行代码到您的~/.zshrc中,zsh将自动运行您在.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

注意:

  • NR>118 适用于 Ubuntu,因为 Ubuntu 默认的 .bashrc 有 118 行,这些行应该被忽略。
  • 您的自定义命令应附加在 .bashrc 的末尾。 不要在系统的 bashrc 区域插入新行

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文