5

I received this excellent answer on how to convert a zsh function to a fish function. Now I have another question. How do I call that function from another function, passing on the argument?

I have tried this:

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

but I get "env: ogf: No such file or directory".

The goal is only to change the EDITOR environment variable for this one execution, and then call ogf.

2 Answers 2

6

The env command can only run other external commands. It cannot call shell builtins or functions; regardless whether the shell is fish, bash, or something else. The solution is to define the function being called with the --no-scope-shadowing flag and use set -l in the calling function:

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
Sign up to request clarification or add additional context in comments.

Comments

1

Another option would be to write your function to use its own arguments, as follows:

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

Maybe this is a simpler example on how to call another function while passing arguments:

function foo
  bar "hello" "world"
end

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

Then calling foo will print:

$ foo
hello
world

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.