如何 运行 .zshrc 中定义的函数与 Ruby 中的系统?

How to run functions defined in .zshrc with system in Ruby?

这是我的 .zshrc:

hello() {
    echo "hello"
}

和我的 test.rb:

system "hello"

而且它不起作用。如何让它发挥作用?

您将无法从 Ruby 调用 shell 函数。 system 调用将生成一个 non-interactive 子进程 Zsh。

注意事项:

  1. .zshrc 仅用于交互式 shell.
  2. 您不能从非 shell 脚本调用 shell 函数。即使在脚本中,它们也需要先 sourced。

您可以将该函数放入一个小脚本中并调用它:

% <hello
#! /usr/bin/env zsh
hello() { echo "hello" }
hello
% chmod +x hello

然后在 test.rb:

system "./hello"