Challenge - 5 Problems
Variable Access Mastery
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 the following Bash script snippet. What will it print when run?
Bash Scripting
name="World" echo "Hello $name!" echo "Hello ${name}!"
Attempts:
2 left
💡 Hint
Both $var and ${var} are ways to access the value of a variable in Bash.
✗ Incorrect
In Bash, $name and ${name} both access the variable 'name'. So both echo commands print 'Hello World!'.
💻 Command Output
intermediate2:00remaining
What is the output when variable names are adjacent to text?
What will this Bash script print?
Bash Scripting
var="day" echo "Good $vartoday" echo "Good ${var}today"
Attempts:
2 left
💡 Hint
Without braces, Bash tries to find a variable named 'vartoday'.
✗ Incorrect
Without braces, $vartoday looks for a variable named 'vartoday' which is empty, so prints 'Good '. With braces, ${var} is 'day', so prints 'Good daytoday'.
📝 Syntax
advanced2:00remaining
Which option correctly accesses a variable followed by a digit?
You have a variable named 'file1'. Which echo command correctly prints its value followed by 'backup'?
Bash Scripting
file1="data.txt"Attempts:
2 left
💡 Hint
Use braces to separate variable name from following text.
✗ Incorrect
Option C uses braces to clearly separate the variable 'file1' from the text 'backup'. Option C looks for variable 'file1backup' which is undefined. Option C prints the variable and then a space and 'backup'. Option C looks for variable 'file1backup' which is undefined.
🔧 Debug
advanced2:00remaining
Why does this script print an empty line?
Given the script below, why does the first echo print an empty line?
Bash Scripting
var="test" echo "$var123" echo "${var}123"
Attempts:
2 left
💡 Hint
Check what variable names Bash tries to expand.
✗ Incorrect
Without braces, $var123 tries to expand variable named 'var123' which is not set, so prints empty. The second echo uses braces to expand 'var' and then adds '123'.
🚀 Application
expert2:00remaining
How many items are in the array after this script runs?
Consider this Bash script. How many elements does the array 'arr' contain after execution?
Bash Scripting
prefix="item" for i in 1 2 3; do arr+=("${prefix}${i}") done echo ${#arr[@]}
Attempts:
2 left
💡 Hint
Each loop adds one element to the array.
✗ Incorrect
The loop runs 3 times, each time adding one element to 'arr'. So the array has 3 elements.