在 bash 脚本中获取我的 .zshrc

发布于 2024-12-27 06:29:50 字数 480 浏览 4 评论 0原文

假设我有这个 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 技术交流群。

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

发布评论

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

评论(2

丘比特射中我 2025-01-03 06:29:50

在您的脚本中,您使用 bash 运行 zsh 脚本。您不妨让 python 解释器解析 perl

在 shebang 行中将 bash 更改为 zsh 或使用 bash 命令编写脚本。

In your script, you're using bash to run a zsh script. You might as well ask the python interpreter to parse perl.

Either change bash to zsh in the shebang line or write the script with bash commands.

岁月染过的梦 2025-01-03 06:29:50

它并不像 python 与 perl 那样糟糕。 bash 和 zsh 均源自 Bourne shell(其行为由 POSIX 标准化),因此任何设计用于使用 /bin/sh 的脚本都可能适用于 bash 或 zsh。

通常,顾名思义,您的 ~/.zshrc 被设计为与 zsh 一起使用,并且通常会包含 zsh 特定的命令,例如 autoloadcompinit< /代码>。

可以使这些命令成为有条件的,例如:

if [ "$ZSH_VERSION" ] ; then
    autoload -U compinit
    compinit
fi

但这当然意味着您将无法获得这些命令的功能,除非您能找到一种在 bash 中模拟它们的方法。 (我对这两个命令都不熟悉,所以我无法帮助您。)

(请注意,如果您执行了 set -uset -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 like autoload and compinit.

You can make those commands conditional, for example:

if [ "$ZSH_VERSION" ] ; then
    autoload -U compinit
    compinit
fi

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 or set -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.)

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