Fish Shell: Functions
Tags: fish, functions, cli, terminal
Published: 2020-04-29 15:46:51

In this article, we will explore how to create and save [fish shell](fishshell.com/) functions on the fly.
This is useful if you need to automate things in the shell.

>Tested on fish version: 3.0.2



# Create a function

Use `funced` [[1]](https://fishshell.com/docs/current/cmds/funced.html) to edit and create a `function`[[2]](https://fishshell.com/docs/current/cmds/function.html#cmd-function) interactively.
It will provide a scaffold to write a function on the terminal.

```fish
$ funced
function hello
    echo hello world
end
```

> Tip: If you have `$EDITOR` configured, it will open that instead


Hitting Enter will save the function `hello` into this session. Running `hello` will echo "hello world".

This function will only be available for this session. It will not be saved for future sessions.



# Save a function 

To save our `hello` function so that we can use it in future sessions, we use `funcsave`[[3]](https://fishshell.com/docs/current/cmds/funcsave.html).

![Saving a function](https://dev-to-uploads.s3.amazonaws.com/i/ucr7oy6qgh8s59u2ts0f.png)

This will save `hello` into a file in our configuration directory, where it will automatically be loaded for future sessions.

You can see this function saved in `~/.config/fish/functions/hello.fish`.

# Make your functions descriptive

It's always good to document our functions, especially if we are writing it with the intent to share with other people. For that, we use the `--description` option.


Use `funced hello` to edit the previously defined `hello` function. Now we can make any changes and use `funcsave` if we want to save it.

Continuing our previous example we can define a description option to set a description for our function. 

![Assigning descriptions to functions](https://dev-to-uploads.s3.amazonaws.com/i/tey2leb35nrx0945o52p.png)

> Tip: you can use `-d` as a shortcut to `--description`

# Function arguments

If we want to make our `hello` function say what we want, we can use arguments! 

In our function, we have access to `$argv` which is a [variable list](https://fishshell.com/docs/current/index.html#variables-lists) of arguments that the function received.

This is a welcomed changed from bash which has numbered variables like `$1` to access the first argument of the function.

If we wanted the first argument of a function, we simply call `$argv[1]`. 

![Using function arguments](https://dev-to-uploads.s3.amazonaws.com/i/1vulykmal7eewa0qumki.png)


> Note: Fish shell indices start at 1, not 0. Fish tries to be modern this way.

# Use named function arguments

To make it more readable, we can use named arguments. This is much friendlier to read compared to `$argv[1]`.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ominmh41ucwxpiynt4am.png)

The `--arguments` or `-a` option allows us to specify variable names for arguments that we specify.

The variable `$greeting` and `$name` correspond to the first and second arguments of the `say` function.


# Learn More

I hope you learned a new thing about the fish shell and how to write your own functions! You can always read more about fish shell functions [here](https://fishshell.com/docs/current/cmds/function.html#cmd-function).

Until then, keep learning!