在鱼壳中调用另一个功能

时间:2017-02-06 23:16:06

标签: fish

我收到了this关于如何将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:没有这样的文件或目录"。

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

2 个答案:

答案 0 :(得分:4)

env命令只能运行其他外部命令。它不能调用shell内置函数或函数;无论外壳是鱼,还是其他东西。解决方案是使用--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

答案 1 :(得分:0)

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

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