0
0
Bash Scriptingscripting~20 mins

Accessing variables ($var and ${var}) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Variable Access Mastery
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 snippet. What will it print when run?
Bash Scripting
name="World"
echo "Hello $name!"
echo "Hello ${name}!"
AHello ${name}!\nHello World!
BHello $name!\nHello ${name}!
CHello World!\nHello $name!
DHello World!\nHello World!
Attempts:
2 left
💡 Hint
Both $var and ${var} are ways to access the value of a variable in Bash.
💻 Command Output
intermediate
2: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"
AGood \nGood daytoday
BGood daytoday\nGood daytoday
CGood daytoday\nGood today
DGood \ntoday\nGood today
Attempts:
2 left
💡 Hint
Without braces, Bash tries to find a variable named 'vartoday'.
📝 Syntax
advanced
2: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"
Aecho "${file1backup}"
Becho "$file1backup"
Cecho "${file1}backup"
Decho "$file1 backup"
Attempts:
2 left
💡 Hint
Use braces to separate variable name from following text.
🔧 Debug
advanced
2: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"
ABecause $var123 looks for variable 'var123' which is not set, so prints empty.
BBecause $var123 is a syntax error and stops the script.
CBecause $var123 prints the value of 'var' and then '123'.
DBecause ${var}123 is invalid syntax.
Attempts:
2 left
💡 Hint
Check what variable names Bash tries to expand.
🚀 Application
expert
2: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[@]}
AError: arr not declared
B3
C1
D0
Attempts:
2 left
💡 Hint
Each loop adds one element to the array.