Lesson 11 of 14 · 10 min
The Environment and Your PATH
You install a bioinformatics tool, type its name, press Enter, and the terminal replies "command not found." The program is sitting right there on your disk, so why can't the shell see it? The shell -- the program that reads and runs the commands you type -- finds programs by consulting a list of folders called PATH, one of many named values that make up its environment, and this lesson shows you how to inspect and fix it.
Variables the Shell Remembers
An environment variable is a named piece of information the shell keeps in memory for you, like a labelled box holding a value. You read a variable by writing a dollar sign in front of its name, and the command echo simply prints whatever you hand it. So echo $HOME asks the shell to print the value stored in the variable named HOME.
echo $HOME/home/student
PATH is a special environment variable that holds a list of folders, separated by colons. Each time you type a command, the shell walks through those folders from left to right and runs the first program whose name matches. The command which performs the same search and prints the full location of the program that would run, so you can see exactly which copy the shell will use.
echo $PATH
which python3/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
/usr/bin/python3
Now suppose you unpacked the tool samtools into a folder named tools inside your home directory; because that folder is not listed in PATH, the shell searches everywhere else, fails, and reports "command not found." The command export adds your folder to the front of PATH for the current session. To make the change permanent, append the same line to ~/.bashrc, a startup file the shell reads every time a new terminal opens, then run source ~/.bashrc to reload it right now. Of the five commands below, only the first (samtools --version) and the third (which samtools) print anything; export, the echo that appends to ~/.bashrc, and source all run silently.
samtools --version
export PATH="$HOME/tools:$PATH"
which samtools
echo 'export PATH="$HOME/tools:$PATH"' >> ~/.bashrc
source ~/.bashrcbash: samtools: command not found
/home/student/tools/samtools
Try it yourself: Run echo $PATH and count the folders separated by colons. Then create a folder with mkdir ~/tools, add it with export PATH="$HOME/tools:$PATH", and run echo $PATH again to confirm your folder now appears at the front of the list. This is the exact reason a freshly installed tool "isn't found" until its folder is on PATH.