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:
| Variable | Description | Example |
|---|---|---|
PATH | List of directories where Linux looks for commands | /usr/local/bin:/usr/bin:/bin |
HOME | Current user’s home directory | /home/pooja |
USER | The current username | pooja |
PWD | Present working directory | /home/pooja/projects |
SHELL | The current shell | /bin/bash |
LANG | Language and locale settings | en_US.UTF-8 |
Checking environment variables
- Use
echoto 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
~/.bashrcor~/.bash_profile:
- Add it to your shell configuration files like
export MY_VAR="hello"
- Note:
exportmakes it available to programs started from the shell.
Practical IT examples
- Setting the
JAVA_HOMEenvironment 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
- Setting
PYTHONPATHfor Python scripts to find modules:
export PYTHONPATH=/home/pooja/my_python_modules
- 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:
- Using a variable inside double quotes:
LOG_DIR="/var/log/myapp"
echo "Logs are stored in $LOG_DIR"
- Preventing expansion with single quotes:
echo 'Logs are stored in $LOG_DIR'
# Output: Logs are stored in $LOG_DIR
- 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
PATHbecause it’s often tested: commands won’t run if they’re not in a directory listed inPATH.
Quick Practice Questions
- How do you permanently set an environment variable for all sessions?
- What will this output?
USER="Alice"
echo 'Hello $USER'
echo "Hello $USER"
- How do you temporarily add
/opt/binto yourPATHvariable for the current session?
Answers:
- Add
export VAR=valueto~/.bashrcor~/.bash_profile.
Hello $USER
Hello Alice
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.
