How to get arguments in Fish script?

· · 132 words · 1 minute read

Here is a command with 3 arguments.

myfunc one two "third arg"

How to get each argument in Fish script/function.

  1. to get all arguments as one string/text, use $argv.
  2. to get first argument, use $argv[1].
  3. to get second argument, use $argv[2].
  4. to get the function name or the script name, use $argv[0].
  5. to the remaining three arguments after two : myfunc one two three four five, use $argv[3..-1] as -1 is the end of arguments.
  6. to echo/print the arguments, use eval like this eval $argv[3..-1].

For example, I created gitp Fish function to shorten the git steps.

function gitp --description "gitp <message>"
    git add -Av
    git commit -m $argv
    git push

I hope this helps you. You know someone will benefit from this, share it with him/her, a friend or colleage.