← Back to Bash Course | Chapter 2: Variables & Types | Lesson 5 of 13

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:

  1. Environment Variables
Syntax
bash
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

bash
#!/bin/bash
echo "Home directory: $HOME"
echo "Current user: $USER"
echo "Shell: $SHELL"
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
  1. Setting a variable in a script and expecting it to appear in your terminal afterwards, since a script runs in a child process.
  2. Setting PATH=/new/dir and losing the old value; write PATH="$PATH:/new/dir".
  3. Assuming variable names are case-insensitive, when Home and HOME are different.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.