Environment variables and quoting

2.1 Command Line Basics (Weight: 3)

📘Linux Essentials (LPI 010-160)


What are environment variables?

  • Environment variables are named values stored in the shell that programs and scripts can use.
  • They define the environment in which commands and programs run.
  • Think of them as settings that tell Linux or applications how to behave.

Common environment variables

Here are the ones you should know:

VariableDescriptionExample
PATHList of directories where Linux looks for commands/usr/local/bin:/usr/bin:/bin
HOMECurrent user’s home directory/home/pooja
USERThe current usernamepooja
PWDPresent working directory/home/pooja/projects
SHELLThe current shell/bin/bash
LANGLanguage and locale settingsen_US.UTF-8

Checking environment variables

  • Use echo to see a variable:
echo $HOME
echo $PATH
  • To see all environment variables:
printenv
env

Setting environment variables

  • Temporary (current shell only):
MY_VAR="hello"
echo $MY_VAR
  • Permanent (available every time you log in):
    • Add it to your shell configuration files like ~/.bashrc or ~/.bash_profile:
export MY_VAR="hello"
  • Note: export makes it available to programs started from the shell.

Practical IT examples

  1. Setting the JAVA_HOME environment variable so Java-based programs can find the Java installation:
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
export PATH=$JAVA_HOME/bin:$PATH
  1. Setting PYTHONPATH for Python scripts to find modules:
export PYTHONPATH=/home/pooja/my_python_modules
  1. Defining a custom log directory for a script:
export LOG_DIR=/var/log/myapp

2. Quoting in Linux

Quoting is used to control how the shell interprets text. This is important when using environment variables, paths, or strings with spaces.

There are three main types of quotes:

2.1 Double Quotes " "

  • Preserves spaces but allows variable and command substitution.
  • Example:
NAME="pooja"
echo "Hello, $NAME"
# Output: Hello, pooja
  • IT Example: passing a directory path with spaces to a script:
DIR="/home/pooja/My Projects"
echo "Processing files in $DIR"

2.2 Single Quotes ' '

  • Preserves everything literally, no variable or command substitution.
  • Example:
echo 'Hello, $NAME'
# Output: Hello, $NAME
  • IT Example: storing a password with special characters in a variable:
PASSWORD='MyP@$$word123'

2.3 Backslash \ (escaping)

  • Escapes a single character to treat it literally.
  • Example:
echo "The file is called My\ File.txt"
  • IT Example: using special characters in commands:
echo "Path with \$ sign: \$HOME"
# Output: Path with $ sign: /home/pooja

3. Combining Environment Variables and Quoting

Knowing how quoting works helps you use environment variables safely in scripts or commands.

Examples:

  1. Using a variable inside double quotes:
LOG_DIR="/var/log/myapp"
echo "Logs are stored in $LOG_DIR"
  1. Preventing expansion with single quotes:
echo 'Logs are stored in $LOG_DIR'
# Output: Logs are stored in $LOG_DIR
  1. Escaping special characters inside variables:
MY_PATH="/home/pooja/My\ Projects"
echo $MY_PATH

4. Exam Tips

  • Always remember the difference between single and double quotes.
  • Know how to view, set, and export environment variables.
  • Be able to use variables inside scripts and commands correctly.
  • Understand PATH because it’s often tested: commands won’t run if they’re not in a directory listed in PATH.

Quick Practice Questions

  1. How do you permanently set an environment variable for all sessions?
  2. What will this output?
USER="Alice"
echo 'Hello $USER'
echo "Hello $USER"
  1. How do you temporarily add /opt/bin to your PATH variable for the current session?

Answers:

  1. Add export VAR=value to ~/.bashrc or ~/.bash_profile.
Hello $USER
Hello Alice
  1. export PATH=/opt/bin:$PATH

This covers everything you need for environment variables and quoting in LPI 010-160. It includes practical IT examples like Java, Python, logs, and scripts, so even non-IT learners can relate.

Buy Me a Coffee