Environment Variables
Environment variables are like signs hanging in the school hallway that every classroom can read, such as who is logged in or where your home folder is. Your scripts can look at them anytime.
In this page:
Syntax
echo "$HOME"
echo "$USER"
echo "$PATH"
Environment Variables
Environment variables like $HOME, $USER, and $PATH are set by the system or shell and are available to every script and program that runs.
They carry configuration and context, such as the current user's home directory. You can read them just like any other variable, with $.
Note:
Run
env or printenv in a terminal to see all environment variables currently set.Example: Environment Variables
#!/bin/bash
echo "Home directory: $HOME"
echo "Current user: $USER"
echo "Shell: $SHELL"
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Setting a variable in a script and expecting it to appear in your terminal afterwards, since a script runs in a child process.
- Setting
PATH=/new/dirand losing the old value; writePATH="$PATH:/new/dir". - Assuming variable names are case-insensitive, when
HomeandHOMEare different.
🔒
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: