Bird
Raised Fist0
Linux CLIscripting~20 mins

Environment variables in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Environment Variable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:00remaining
What is the output of this command?
You run this command in a Linux shell:

echo $HOME

What will it output?
Linux CLI
echo $HOME
AThe string "$HOME" literally
BThe path to the current user's home directory, e.g., /home/username
CAn empty line
DThe current working directory path
Attempts:
2 left
💡 Hint
Environment variables start with $ and store system info.
💻 Command Output
intermediate
1:00remaining
What does this command output?
Given you run these commands in order:

export MYVAR=hello
echo $MYVAR

What will the second command output?
Linux CLI
export MYVAR=hello
echo $MYVAR
Aexport MYVAR=hello
B$MYVAR
CAn empty line
Dhello
Attempts:
2 left
💡 Hint
export makes the variable available to child processes.
📝 Syntax
advanced
1:30remaining
Which option correctly sets an environment variable for the current shell session?
You want to set an environment variable named PATH_EXT to "/usr/local/bin" for your current shell session only. Which command is correct?
Aexport PATH_EXT=/usr/local/bin
BPATH_EXT=/usr/local/bin
Cset PATH_EXT=/usr/local/bin
Denv PATH_EXT=/usr/local/bin
Attempts:
2 left
💡 Hint
Use export to make variable available in the current shell.
💻 Command Output
advanced
1:00remaining
What error does this command produce?
You run this command:

echo $UNDEFINED_VAR

Assuming UNDEFINED_VAR was never set, what is the output?
Linux CLI
echo $UNDEFINED_VAR
AAn empty line
BUNDEFINED_VAR
Cbash: UNDEFINED_VAR: command not found
D$UNDEFINED_VAR
Attempts:
2 left
💡 Hint
Undefined variables expand to empty strings in bash.
🚀 Application
expert
1:30remaining
How many environment variables are listed by this command?
You run this command in a Linux shell:

env | wc -l

What does the output number represent?
Linux CLI
env | wc -l
AThe number of shell functions defined
BThe number of files in the current directory
CThe number of environment variables currently set
DThe number of running processes
Attempts:
2 left
💡 Hint
env lists environment variables, wc -l counts lines.

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

  1. Step 1: Understand environment variables

    Environment variables hold information like settings or preferences that programs can read while running.
  2. Step 2: Identify the correct purpose

    Among the options, only storing settings matches the role of environment variables.
  3. Final Answer:

    To store settings that programs can use during execution -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    export MY_VAR=hello -> Option D
  4. 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?
GREETING="Hello"
export GREETING
GREETING="Hi"
echo $GREETING
medium
A. Hello
B. $GREETING
C. Hi
D. Error: variable not found

Solution

  1. Step 1: Understand variable assignment and export

    First, GREETING is set to "Hello" and exported, making it available to child processes. Then GREETING is reassigned to "Hi" in the current shell.
  2. Step 2: Determine the value echoed

    The echo command prints the current value of GREETING, which is "Hi" after reassignment.
  3. Final Answer:

    Hi -> Option C
  4. 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

  1. 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.
  2. Step 2: Check export usage

    Exporting PATH after assignment makes the new value available to child processes. This is correct usage.
  3. Final Answer:

    Nothing is wrong; it correctly appends to PATH -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    TEMP_VAR=value command -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard