如何将.bashrc导出到.zshrc?

Léo*_* 준영 56 bash zsh zshrc

我正试图从Bash转移到zsh.

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

如何将.bashrc导出到.zshrc?

Ryn*_*ett 78

虽然lhunath的回答让我朝着正确的方向发展,但zsh似乎并没有.profile自动获取.可以在此超级用户帖子上找到关于主题的很多信息.

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

的.bashrc

source ~/.profile

.zshrc

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

  • 是的,我认为这是最好的方法。谢谢您的回答! (2认同)

lhu*_*ath 27

你不能" 导出 "你.bashrc.zshrc. .bashrc是一个运行bash命令的文件. .zshrc是一个运行zsh命令的文件.

您不能期望zsh能够bash在您的命令中运行命令.bashrc,因此您应该将其转换为新命令,.zshrc而不是尝试.bashrc.zshrc前者运行或将前者复制到后者中.

如果你想要所有shell的公共shell初始化文件; 使用.profile(和删除.bashrc.zshrc).它来自所有POSIX shell.而在那里,坚持POSIX Shell功能.然后该代码将在任何POSIX shell中运行.(虽然,我不是100%肯定zsh是POSIX兼容的).

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

虽然 - 而且我首先误读了你问题的这一部分 - 除非你把命令放在那里,否则你不应该bash在运行时遇到错误.你是否?你得到了什么错误?听起来像你已经添加了代码并且(显然)不明白..bashrczshzsh.bashrcbash

另外,ojblass试图提出一个只能部分成功的便携性. zsh是一个伟大的外壳(虽然我自己没有荣誉),但在编写脚本时; 我建议你这样做#!/usr/bin/env bash.主要是为了您自己(最终,与您共享的人)的便携性.


小智 6

对我来说,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'
Run Code Online (Sandbox Code Playgroud)

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

在 bash shell 中(vim ~/.bashrc)

source ~/.profile
Run Code Online (Sandbox Code Playgroud)

在 zsh shell 中( vim ~/.zshrc )

source ~/.profile
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考,您可以将所有 git 特定别名添加到 ~/.gitconfig 中。然后你可以添加有趣的东西,比如 `publish = "!pub() { git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD);}; pub"` (2认同)

Moh*_*rbi 5

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

\n

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

\n

gedit ~/.zshrc :

\n

...并在末尾添加以下内容:

\n
...\n# Alias definitions.\n# You may want to put all your additions into a separate file like\n# ~/.bash_aliases, instead of adding them here directly.\n# See /usr/share/doc/bash-doc/examples in the bash-doc package.\n    \nif [ -f ~/.bash_aliases ]; then\n    . ~/.bash_aliases\nfi\n
Run Code Online (Sandbox Code Playgroud)\n

完成后运行:

\n
source ~/.zshrc\n
Run Code Online (Sandbox Code Playgroud)\n

瞧\xc3\xa0...现在你已经在 bash 和 zsh 之间共享了别名。

\n