Skip to content

Variables

A variable is a name that stores a value, so you can use it later.

Terminal window
set name Obay
echo "hi $name"
# hi Obay

In fish you write set, then the name, then the value. To read it back, put a $ in front.

A variable can also hold several values.

Terminal window
set colors red green blue
echo $colors[2]
# green
count $colors
# 3

In fish, every variable is a list. A normal variable is just a list with one thing in it. Counting starts at 1.

Your shell starts with some variables already set. These are called environment variables, and they’re usually written in capital letters:

Terminal window
echo $HOME # your home folder
echo $USER # your username
echo $PATH # where the shell looks for commands

$PATH is the list of folders where the shell looks for programs. When you type ls, the shell goes through those folders one by one until it finds a program called ls. You’ll use this in later chapters.

To make a variable visible to the programs you run, add -x (for export):

Terminal window
set -x EDITOR nano

That lasts until you close the terminal. To keep it after that, add -U (for universal) as well:

Terminal window
set -Ux EDITOR nano

Since variables are text, you need a command to do math:

Terminal window
math 2 + 3
# 5
math 10 / 4
# 2.5