Challenge - 5 Problems
Variable Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this Bash script?
Consider this Bash script that uses a variable to store a value and reuse it later. What will it print?
Bash Scripting
name="Alice" echo "Hello, $name!" name="Bob" echo "Hello again, $name!"
Attempts:
2 left
💡 Hint
Variables hold the value assigned at the time of use.
✗ Incorrect
The variable 'name' is first set to 'Alice', so the first echo prints 'Hello, Alice!'. Then it is changed to 'Bob', so the second echo prints 'Hello again, Bob!'.
🧠 Conceptual
intermediate1:30remaining
Why do we use variables in scripts?
Which of these best explains why variables are used in scripting?
Attempts:
2 left
💡 Hint
Think about how you remember information to use it again.
✗ Incorrect
Variables let scripts hold data in memory so it can be reused or updated without rewriting the data each time.
🔧 Debug
advanced2:00remaining
Why does this script print an empty line?
Look at this Bash script:
```
message=Hello
message
```
Why does it print an empty line instead of 'Hello'?
Attempts:
2 left
💡 Hint
Think about how to print text in Bash.
✗ Incorrect
Just writing the variable name alone does not print its value. You must use 'echo $message' to display it.
📝 Syntax
advanced1:30remaining
Which option correctly assigns and reuses a variable in Bash?
Choose the correct Bash code that assigns '5' to a variable and then prints it.
Attempts:
2 left
💡 Hint
Remember, no spaces around '=' and use $ to access variable value.
✗ Incorrect
In Bash, variable assignment must have no spaces around '=', and to print the value, use '$' before the variable name.
🚀 Application
expert2:30remaining
What is the value of 'result' after running this script?
Given this Bash script:
```
count=3
result=$((count * 2))
count=5
result=$((result + count))
echo $result
```
What number will be printed?
Attempts:
2 left
💡 Hint
Follow the math step by step, updating variables as you go.
✗ Incorrect
Initially, count=3, so result=3*2=6. Then count changes to 5, and result becomes 6+5=11.