Shell variables
You have already used a variable without making one: $HOME, which the shell fills in with the path to your home directory. You can make your own just as easily.
$ ref=/data/reference/hg38.fa
Note that there are no spaces around the =. This is the single most common mistake when starting out, because it looks so much like other languages. With spaces, the shell reads ref as a command name:
$ ref = /data/reference/hg38.fa ref: command not found
To use the value, put a $ in front of the name:
$ echo $ref /data/reference/hg38.fa $ ls -l $ref
Variables are just text substitution. The shell replaces $ref with the value before the command runs, exactly like it does with wildcards.
Quote your variables. If a value contains a space, an unquoted variable gets split into several arguments, the same problem you saw with filenames:
$ dir="my project" $ ls $dir looks for two directories, "my" and "project" $ ls "$dir" looks for one directory named "my project"
Get into the habit of writing "$dir" with quotes. It costs nothing and saves a lot of confusion later.
Sometimes you need to make clear where the name ends. Curly braces do that:
$ sample=patient1
$ echo $sample_reads.fastq
.fastq
$ echo ${sample}_reads.fastq
patient1_reads.fastq
The first one lost the sample name entirely, because the shell looked for a variable called sample_reads, which does not exist, and put nothing in its place. Only the literal .fastq survived. An undefined variable quietly becomes empty rather than raising an error, which is worth remembering when a command behaves strangely.
You can also capture the output of a command into a variable, by putting it in $( ):
$ today=$(date +%Y-%m-%d) $ echo $today 2026-08-10 $ count=$(wc -l < samples.tsv) $ echo "there are $count samples"
This is enormously useful, and it is how you build filenames that include a date, a sample name, or a count.
Finally, a variable you set like this exists only in your current shell. It is not passed to programs you run, and it disappears when you log out. Making it stick is what the export command is for, and we come back to that in the section on your environment.
Exercise
- Set a variable to a directory you use often and cd into it using the variable.
- Set a variable containing a space, then run ls on it with and without quotes and compare the errors.
- Use $(date +%Y-%m-%d) to build a filename that includes today's date, and create it with touch.
Quiz Question
What is wrong with the assignment: name = pete
Show answer
there must be no spaces around the =