在 fish shell 中调用另一个函数

Calling another function in fish shell

我收到了 关于如何将 zsh 函数转换为 fish 函数的优秀答案。现在我有另一个问题。如何从另一个函数调用该函数并传递参数?

我试过这个:

function ogf
  echo "Cloning, your editor will open when clone has completed..."
  source (env TARGET_DIRECTORY=~/students EDITOR=$EDITOR clone_git_file -ts $argv[1] | psub)
end

function wogf
  env EDITOR=webstorm ogf "$argv[1]"
end

但我得到 "env: ogf: No such file or directory"。

目标只是为这一次执行更改EDITOR环境变量,然后调用ogf

env命令只能运行其他外部命令。它不能调用 shell 内置函数或函数;不管 shell 是鱼、bash 还是其他东西。解决方法是用--no-scope-shadowing标志定义被调用的函数,并在调用函数中使用set -l

function ogf --no-scope-shadowing
  echo "Cloning, your editor will open when clone has completed..."
  source (env TARGET_DIRECTORY=~/students EDITOR=$EDITOR clone_git_file -ts $argv[1] | psub)
end

function wogf
  set -l EDITOR webstorm
  ogf $argv
end

另一种选择是编写您的函数以使用其自己的参数,如下所示:

function ogf
  echo "Cloning, your editor will open when clone has completed..."
  source (env TARGET_DIRECTORY=~/students EDITOR=$argv[2] clone_git_file -ts $argv[1] | psub)
end

function wogf
  ogf $argv[1] 'webstorm'
end

也许这是一个关于如何在传递参数时调用另一个函数的更简单的示例:

function foo
  bar "hello" "world"
end

function bar
  echo $argv[1]
  echo $argv[2]
end

然后调用 foo 将打印:

$ foo
hello
world