Linux Terminal FundamentalsLesson 2.4
How to use environment variables and the PATH in Linux
environment variables, echo $VAR, export command, PATH variable, which command, permanent vs session-only variables, .bashrc and .zshrc
Environment Variables and PATH
Environment variables are key-value pairs the shell and every program it launches can read. PATH is the most important one โ it controls which programs you can run by name.
Reading variables
echo $HOME
echo $USER
echo $PATHSetting a variable for the current session
export MY_API_KEY=abc123
echo $MY_API_KEYVariables set with export are visible to child processes. Without export, they are only visible in the current shell.
Making variables permanent
Add export lines to ~/.bashrc then reload:
echo 'export MY_API_KEY=abc123' >> ~/.bashrc
source ~/.bashrcFinding where a command lives
which node
which python3When a command is not found, the binary is either not installed or its directory is not in $PATH. Add it permanently:
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc