The PATH Variable
Part of the Fundamentals section of Coddy's Terminal journey — lesson 61 of 82.
The PATH variable is one of the most important environment variables in your system. It tells the shell where to look for executable programs when you type a command.
When you run a command like ls or cat, the shell doesn't magically know where these programs are stored. It searches through a list of directories defined in PATH. View it with:
echo $PATHYou'll see something like this:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbinEach directory is separated by a colon (:). When you type ls, the shell checks each directory in order: first /usr/local/bin, then /usr/bin, and so on until it finds the ls program.
If a program isn't in any of these directories, you'll get a "command not found" error. This is why understanding PATH matters.
When you install new software or create your own scripts, they need to be in a directory listed in PATH, or you'll have to type the full path to run them.
You can find exactly where a command lives using which:
which lsThis shows the full path to the program that runs when you type that command.
Challenge
EasyUse the which command to find the full path to the cat program.
The which command searches through the directories listed in your PATH variable and shows you exactly where a command's executable file is located.
Your output should display the full path to the cat program (something like /usr/bin/cat or /bin/cat).
Hint: The command format is
which command_name
Cheat sheet
The PATH variable tells the shell where to look for executable programs. View it with:
echo $PATHThe output shows directories separated by colons (:):
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbinWhen you run a command, the shell searches through these directories in order until it finds the program.
To find the exact location of a command, use which:
which lsTry it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline12Environment
Environment VariablesView All VariablesThe PATH VariableSetting A VariableExport A VariableThe Profile FileRecap - Custom Environment