DEV Community

Cover image for Fish Shell: Functions
Jethro Lorenzo Garcia Lising
Jethro Lorenzo Garcia Lising

Posted on

Fish Shell: Functions

In this article, we will explore how to create and save fish shell 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] to edit and create a function[2] interactively.
It will provide a scaffold to write a function on the terminal.

$ 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].

Saving a function

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

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 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

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

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.

Until then, keep learning!

Top comments (2)

Collapse
 
tarikwaleed profile image
Tarik Waleed

Thanks for this great tutorial.
here is a function i created to change the brightness of the laptop from the command line(tested on Ubuntu)

function dim --argument value
    echo $value | sudo tee /sys/class/backlight/intel_backlight/brightness
end
Enter fullscreen mode Exit fullscreen mode
Collapse
 
iqbalpb profile image
Muhammed Iqbal P.B

How did you create this command screenshots?