在 bash 脚本中获取我的 .zshrc
假设我有这个 bash 脚本(测试):
#!/usr/bin/env bash
source ~/.zshrc
在我的 .zshrc 中,我有以下内容:
autoload -U compinit
compinit
当我尝试从终端窗口 (zsh) 运行“bash 测试”时,出现错误,提示未找到 autoload 和 compinit 命令。如果我只是从命令行执行 source ~/.zshrc ,它就可以正常工作。
我正在尝试设置我的开发环境,类似于 blog,但是当脚本尝试获取 .zshrc 文件时,它会失败。
任何见解将不胜感激。
Lets say I have this bash script (test):
#!/usr/bin/env bash
source ~/.zshrc
In my .zshrc, I have the following:
autoload -U compinit
compinit
When I try and run 'bash test' from my terminal window (zsh), I get errors saying autoload and compinit commands are not found. If I just do source ~/.zshrc from the command line, it works fine.
I am trying to setup my development environment, similar to this blog, but when the scripts try and source the .zshrc file it fails.
Any insight would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的脚本中,您使用
bash
运行zsh
脚本。您不妨让python
解释器解析perl
。在 shebang 行中将
bash
更改为zsh
或使用bash
命令编写脚本。In your script, you're using
bash
to run azsh
script. You might as well ask thepython
interpreter to parseperl
.Either change
bash
tozsh
in the shebang line or write the script withbash
commands.它并不像 python 与 perl 那样糟糕。 bash 和 zsh 均源自 Bourne shell(其行为由 POSIX 标准化),因此任何设计用于使用
/bin/sh
的脚本都可能适用于 bash 或 zsh。通常,顾名思义,您的
~/.zshrc
被设计为与 zsh 一起使用,并且通常会包含 zsh 特定的命令,例如autoload
和compinit< /代码>。
您可以使这些命令成为有条件的,例如:
但这当然意味着您将无法获得这些命令的功能,除非您能找到一种在 bash 中模拟它们的方法。 (我对这两个命令都不熟悉,所以我无法帮助您。)
(请注意,如果您执行了
set -u
或set -o nounset< /code> 在你的 bash shell 中。)
但是如果你要同时使用 zsh 和 bash,那么拥有单独的
~/.bashrc
和~/ 可能更有意义.zshrc
文件,并仅使用每个文件及其设计的外壳。如果您想避免重复,每个人都可以获取包含常用命令的第三个文件。(根据评论,您可能一开始就做错了。)
It's not quite as bad as python vs. perl. Both bash and zsh are derived from the Bourne shell (whose behavior is standardized by POSIX), so any script designed to work with
/bin/sh
is likely to work with either bash or zsh.Normally your
~/.zshrc
, as the name implies, is designed to be used with zsh, and will typically include zsh-specific commands likeautoload
andcompinit
.You can make those commands conditional, for example:
But of course that means you won't get the functionality of those commands, unless you can figure out a way to emulate them in bash. (I'm not familiar with either command, so I can't help you there.)
(Note that this will fail if you've done
set -u
orset -o nounset
in your bash shell.)But if you're going to be using both zsh and bash, it probably makes a lot more sense to have separate
~/.bashrc
and~/.zshrc
files, and use each one only with the shell for which it's designed. If you want to avoid duplication, each one can source a third file containing common commands.(And based on the comments, it's likely you're doing the wrong thing in the first place.)