An environment variable is a named value stored by the operating system that programs can use to get information about the system or user settings. It acts like a note that programs can read to know how to behave.
Click to reveal answer
beginner
How do you display all environment variables in a Linux shell?
You can use the command printenv or env to list all environment variables currently set in your shell session.
Click to reveal answer
beginner
How do you set a new environment variable for the current shell session?
Use the command export NAME=value. For example, export MYVAR=hello sets an environment variable named MYVAR with the value 'hello' for the current session.
Click to reveal answer
intermediate
What happens to environment variables when you close the terminal?
Environment variables set in a terminal session are lost when you close that terminal unless saved in configuration files like ~/.bashrc or ~/.profile to load automatically on new sessions.
Click to reveal answer
beginner
How can you use an environment variable inside a shell script?
You can access an environment variable in a shell script by prefixing its name with a dollar sign, like $VARIABLE_NAME. For example, echo $HOME prints the home directory path.
Click to reveal answer
Which command shows all environment variables in Linux?
Amkdir
Bls
Ccd
Dprintenv
✗ Incorrect
The printenv command lists all environment variables. Other commands like ls, cd, and mkdir do not show environment variables.
How do you set an environment variable named PATH to '/usr/bin' for the current session?
Aexport PATH=/usr/bin
Bset PATH=/usr/bin
CPATH=/usr/bin
Denv PATH=/usr/bin
✗ Incorrect
Use export PATH=/usr/bin to set and export the variable so child processes can see it. Just PATH=/usr/bin sets it only for the current shell, not exported.
What symbol do you use to access the value of an environment variable in a shell script?
A#
B$
C&
D*
✗ Incorrect
The dollar sign $ is used to access the value of an environment variable, e.g., $HOME.
Where should you save environment variable settings to make them permanent for your user?
A/tmp
B/etc/passwd
C~/.bashrc
D/var/log
✗ Incorrect
User-specific environment variables are often saved in ~/.bashrc or ~/.profile to load automatically on new shell sessions.
If you set an environment variable in one terminal, will it be available in a new terminal window automatically?
ANo, unless saved in config files
BOnly if you reboot
CYes, always
DOnly if you use sudo
✗ Incorrect
Environment variables set in one terminal session are not available in new terminals unless saved in configuration files like ~/.bashrc.
Explain what environment variables are and how they are used in Linux.
Think about how programs know about your system or preferences.
You got /4 concepts.
Describe the steps to create a new environment variable that lasts across terminal sessions.
Consider both temporary and permanent ways.
You got /3 concepts.
Practice
(1/5)
1. What is the purpose of environment variables in a Linux shell?
easy
A. To speed up the internet connection
B. To store settings that programs can use during execution
C. To permanently delete files from the system
D. To create new user accounts automatically
Solution
Step 1: Understand environment variables
Environment variables hold information like settings or preferences that programs can read while running.
Step 2: Identify the correct purpose
Among the options, only storing settings matches the role of environment variables.
Final Answer:
To store settings that programs can use during execution -> Option B
Quick Check:
Environment variables = store settings [OK]
Hint: Remember: environment variables hold settings, not files or users [OK]
Common Mistakes:
Confusing environment variables with file operations
Thinking environment variables control hardware
Assuming environment variables create users
2. Which command correctly sets an environment variable named MY_VAR with value hello for the current shell and its child processes?
easy
A. declare MY_VAR hello
B. set MY_VAR=hello
C. env MY_VAR hello
D. export MY_VAR=hello
Solution
Step 1: Recall how to set environment variables
In Linux shells, export is used to set environment variables for the current shell and its children.
Step 2: Check each command
set MY_VAR=hello is incorrect syntax; env MY_VAR hello does not set variable persistently; declare MY_VAR hello is invalid for environment variables.
Final Answer:
export MY_VAR=hello -> Option D
Quick Check:
Use export to set environment variables [OK]
Hint: Use 'export' to set environment variables for shell and children [OK]
Common Mistakes:
Using 'set' instead of 'export' to set environment variables
Trying to set variables without export, so children don't see them
Confusing 'env' command usage
3. Given the commands below, what will be the output of echo $GREETING?
First, GREETING is set to "Hello" and exported, making it available to child processes. Then GREETING is reassigned to "Hi" in the current shell.
Step 2: Determine the value echoed
The echo command prints the current value of GREETING, which is "Hi" after reassignment.
Final Answer:
Hi -> Option C
Quick Check:
Variable reassignment updates value = Hi [OK]
Hint: Last assignment before echo is the value printed [OK]
Common Mistakes:
Thinking export freezes the variable value
Expecting the first assigned value to print
Confusing export with immediate output
4. What is wrong with this script snippet if the goal is to set PATH to include /usr/local/bin?
PATH=$PATH:/usr/local/bin
export PATH
medium
A. Nothing is wrong; it correctly appends to PATH
B. It should use export PATH=$PATH:/usr/local/bin in one line
C. It overwrites PATH instead of appending
D. It should prepend /usr/local/bin before $PATH
Solution
Step 1: Analyze the PATH modification
The script appends :/usr/local/bin to the existing PATH variable, which is a common way to add directories.
Step 2: Check export usage
Exporting PATH after assignment makes the new value available to child processes. This is correct usage.
Final Answer:
Nothing is wrong; it correctly appends to PATH -> Option A
Quick Check:
Appending and exporting PATH is correct [OK]
Hint: Appending PATH then exporting is standard practice [OK]
Common Mistakes:
Thinking export must be on same line as assignment
Confusing append with overwrite
Assuming order of PATH parts always matters
5. You want to create a script that sets a temporary environment variable TEMP_VAR only for a single command execution, without affecting the shell's environment afterwards. Which command correctly does this?
hard
A. TEMP_VAR=value command
B. export TEMP_VAR=value; command
C. set TEMP_VAR=value command
D. command; export TEMP_VAR=value
Solution
Step 1: Understand temporary environment variables for one command
Setting a variable before a command like TEMP_VAR=value command runs the command with that variable set only for its execution.
Step 2: Evaluate each option
export TEMP_VAR=value; command exports variable globally; set TEMP_VAR=value command uses invalid syntax; command; export TEMP_VAR=value sets variable after command runs.
Final Answer:
TEMP_VAR=value command -> Option A
Quick Check:
Prefix variable before command for temporary env [OK]
Hint: Put variable before command to set it temporarily [OK]
Common Mistakes:
Using export which affects shell permanently
Trying to set variable after command runs
Using incorrect syntax like 'set' for env variables
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.