Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Variable assignment (no spaces around =) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bash Variable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Bash script?
Consider the following Bash script. What will it print when run?
Bash Scripting
VAR=Hello
echo "$VAR"
Aecho $VAR
BVAR=Hello
CHello
DSyntax error
Attempts:
2 left
💡 Hint
Remember, in Bash, variable assignment must not have spaces around the equals sign.
💻 Command Output
intermediate
2:00remaining
What error does this Bash script produce?
What happens when you run this script? MYVAR = World echo "$MYVAR"
Bash Scripting
MYVAR = World
echo "$MYVAR"
Abash: MYVAR: command not found
BMYVAR = World
CWorld
DSyntax error
Attempts:
2 left
💡 Hint
Check if spaces around = are allowed in Bash variable assignment.
📝 Syntax
advanced
2:00remaining
Which option correctly assigns a variable in Bash?
Choose the correct way to assign the value '42' to the variable NUMBER in Bash.
ANUMBER = 42
BNUMBER =42
CNUMBER= 42
DNUMBER=42
Attempts:
2 left
💡 Hint
No spaces are allowed around the equals sign in Bash variable assignment.
🚀 Application
advanced
2:00remaining
What is the value of VAR after running this script?
Given this script, what is the value of VAR printed? VAR=foo VAR=bar VAR=baz echo "$VAR"
Bash Scripting
VAR=foo
VAR=bar
VAR=baz
echo "$VAR"
Afoo
Bbaz
Cbar
DSyntax error
Attempts:
2 left
💡 Hint
The last assignment to a variable overwrites previous ones.
🔧 Debug
expert
3:00remaining
Why does this Bash script fail to assign the variable correctly?
Examine the script below and identify why the variable assignment does not work as expected. MYVAR=Hello World echo "$MYVAR"
Bash Scripting
MYVAR=Hello World
echo "$MYVAR"
AThe variable is assigned only 'Hello', and 'World' is treated as a command causing an error.
BThe variable MYVAR is assigned the full string 'Hello World' correctly.
CThe script prints 'Hello World' without errors.
DThe variable assignment syntax is correct and works as intended.
Attempts:
2 left
💡 Hint
In Bash, spaces in variable assignment need special handling.

Practice

(1/5)
1. Which of the following is the correct way to assign the value hello to a variable named greet in bash?
easy
A. greet =hello
B. greet = hello
C. greet= hello
D. greet=hello

Solution

  1. Step 1: Understand bash variable assignment rules

    Bash does not allow spaces around the = sign when assigning variables.
  2. Step 2: Check each option for spaces

    Only greet=hello has no spaces around =. Options B, C, and D have spaces, which cause errors.
  3. Final Answer:

    greet=hello -> Option D
  4. Quick Check:

    No spaces around = means correct assignment [OK]
Hint: No spaces around = in bash variable assignment [OK]
Common Mistakes:
  • Adding spaces around = causes syntax errors
  • Using quotes unnecessarily in simple assignments
  • Trying to assign with spaces after =
2. Which of the following bash commands correctly assigns the value 42 to the variable num?
easy
A. num=42
B. num= 42
C. num =42
D. num = 42

Solution

  1. Step 1: Identify spaces around the equal sign

    Bash requires no spaces around the = sign for variable assignment.
  2. Step 2: Verify each option

    Only num=42 has no spaces around =. Others have spaces causing errors.
  3. Final Answer:

    num=42 -> Option A
  4. Quick Check:

    No spaces around = means correct syntax [OK]
Hint: Remember: no spaces around = in bash assignments [OK]
Common Mistakes:
  • Leaving spaces around = causes syntax errors
  • Confusing assignment with comparison operators
  • Using quotes unnecessarily for numbers
3. What will be the output of the following bash script?
name=John
echo $name
medium
A. name
B. Syntax error
C. John
D. $name

Solution

  1. Step 1: Understand variable assignment and usage

    The variable name is assigned the value John without spaces around =.
  2. Step 2: Understand echo with variable

    echo $name prints the value stored in name, which is John.
  3. Final Answer:

    John -> Option C
  4. Quick Check:

    Variable value printed, not variable name [OK]
Hint: Use $ before variable to print its value [OK]
Common Mistakes:
  • Printing variable name instead of value
  • Forgetting $ before variable in echo
  • Adding spaces around = causing errors
4. Identify the error in the following bash script:
count =10
echo $count
medium
A. Spaces around = cause syntax error
B. Missing $ in echo
C. Variable name is invalid
D. No error, script runs fine

Solution

  1. Step 1: Check variable assignment syntax

    The assignment count =10 has a space before =, which is not allowed in bash.
  2. Step 2: Understand effect of spaces

    Spaces around = cause bash to treat count as a command, leading to syntax error.
  3. Final Answer:

    Spaces around = cause syntax error -> Option A
  4. Quick Check:

    No spaces around = means valid assignment [OK]
Hint: No spaces around = in assignment to avoid errors [OK]
Common Mistakes:
  • Adding spaces around = in assignment
  • Forgetting $ when printing variable
  • Assuming variable names can have spaces
5. You want to store the current year in a variable year using the command date +%Y. Which of the following is the correct way to assign this output to year in bash?
hard
A. year = $(date +%Y)
B. year=$(date +%Y)
C. year= $(date +%Y)
D. year= '$(date +%Y)'

Solution

  1. Step 1: Understand command substitution syntax

    To assign command output, use $(command) without spaces around =.
  2. Step 2: Check each option for spaces and quotes

    year=$(date +%Y) has no spaces around = and uses correct command substitution syntax. Options A and C have spaces causing errors. year= '$(date +%Y)' uses quotes which are unnecessary and may cause literal assignment.
  3. Final Answer:

    year=$(date +%Y) -> Option B
  4. Quick Check:

    No spaces and correct command substitution [OK]
Hint: Use $(command) with no spaces around = for command output assignment [OK]
Common Mistakes:
  • Adding spaces around = causes errors
  • Using quotes around command substitution
  • Forgetting to use $( ) for command output
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