Accessing variables ($var and ${var}) in Bash scripting
📖 Scenario: You are writing a simple bash script to greet users and show their favorite fruit. You want to learn how to use variables correctly in bash scripts.
🎯 Goal: Build a bash script that stores a user's name and favorite fruit in variables, then prints a greeting using both $var and ${var} syntax.
📋 What You'll Learn
Create variables with exact names and values
Use both $var and ${var} syntax to access variables
Print the greeting message exactly as instructed
💡 Why This Matters
🌍 Real World
Using variables is essential in bash scripts to store and reuse information like user input, file names, or configuration values.
💼 Career
Knowing how to access variables correctly is a fundamental skill for system administrators, DevOps engineers, and anyone automating tasks with shell scripts.
Progress0 / 4 steps
1
Create variables for name and fruit
Create a variable called name and set it to "Alice". Create another variable called fruit and set it to "apple".
Bash Scripting
Hint
Use name="Alice" and fruit="apple" to create the variables.
2
Create a greeting message using $var syntax
Create a variable called greeting1 and set it to the string Hello, $name! Your favorite fruit is $fruit. using the $var syntax to access the variables.
Bash Scripting
Hint
Use double quotes and $name, $fruit inside the string.
3
Create a greeting message using ${var} syntax
Create a variable called greeting2 and set it to the string Hello, ${name}! Your favorite fruit is ${fruit}. using the ${var} syntax to access the variables.
Bash Scripting
Hint
Use double quotes and ${name}, ${fruit} inside the string.
4
Print both greeting messages
Print the variables greeting1 and greeting2 each on its own line using echo.
Bash Scripting
Hint
Use echo "$greeting1" and echo "$greeting2" to print the messages.
Practice
(1/5)
1. In bash scripting, what is the main reason to use ${var} instead of just $var?
easy
A. To clearly separate the variable name from surrounding text
B. To declare a new variable
C. To comment out the variable
D. To convert the variable to uppercase
Solution
Step 1: Understand variable access syntax
In bash, $var accesses the value of a variable named var.
Step 2: Purpose of braces
Using ${var} helps separate the variable name from other text, avoiding confusion when concatenating.
Final Answer:
To clearly separate the variable name from surrounding text -> Option A
Quick Check:
Braces separate variable name from text = A [OK]
Hint: Use braces to avoid mixing variable name with text [OK]
Common Mistakes:
Thinking braces declare variables
Using braces to comment variables
Assuming braces change variable case
2. Which of the following is the correct way to print the value of variable name followed immediately by the string _end in bash?
easy
A. echo $name_end
B. echo $name _end
C. echo ${name_end}
D. echo ${name}_end
Solution
Step 1: Understand variable and string concatenation
To print variable name followed by _end, we must separate the variable name from the string.
Step 2: Use braces to separate variable name
Using ${name}_end correctly prints the variable value plus the string _end. Without braces, $name_end looks for a variable named name_end.
Final Answer:
echo ${name}_end -> Option D
Quick Check:
Braces separate variable name from text = C [OK]
Hint: Use braces when adding text right after variable [OK]
A. Hello world!
Hello world!
Hello world123
Hello 123
B. Hello world!
Hello world!
Hello
Hello world123
C. Hello world!
Hello world!
Hello
Hello 123
D. Hello world!
Hello world!
Hello world123
Hello world123
Solution
Step 1: Analyze each echo line
First two lines print "Hello world!" because $var and ${var} both expand to "world". Third line prints "Hello " because $var123 looks for variable named var123 which is empty. Fourth line prints "Hello world123" because ${var} is "world" plus "123".
Hello world!
Hello world!
Hello
Hello world123 -> Option B
Quick Check:
Braces separate variable name correctly = D [OK]
Hint: Braces avoid variable name confusion with suffixes [OK]
Common Mistakes:
Assuming $var123 expands var plus 123
Ignoring empty variable var123
Confusing output lines order
4. Given this script snippet:
filename="report"
echo "File: $filename.txt"
Why does it not print File: report.txt as expected?
medium
A. Because echo cannot print variables with dots
B. Because .txt is treated as a command
C. Because $filename.txt looks for variable named filename.txt which is undefined
D. Because variable names cannot contain letters
Solution
Step 1: Understand variable expansion with suffix
Using $filename.txt makes bash look for variable named filename.txt, which does not exist.
Step 2: Fix with braces
Using ${filename}.txt correctly expands filename and appends .txt.
Final Answer:
Because $filename.txt looks for variable named filename.txt which is undefined -> Option C
Quick Check:
Braces needed to separate variable name from suffix = A [OK]
Hint: Use braces when adding dot or special chars after variable [OK]
Common Mistakes:
Thinking dot is part of variable name
Assuming echo can't print dots
Ignoring variable name rules
5. You want to create a bash script that prints the value of variable user followed by the string _backup only if user is not empty. Which is the best way to write the echo command?
hard
A. echo "${user:+${user}_backup}"
B. echo "${user}_backup"
C. echo "${user}_backup" && [ -n "$user" ]
D. echo "$user_backup"
Solution
Step 1: Understand conditional variable expansion
The expression ${user:+${user}_backup} prints ${user}_backup only if user is not empty.
Step 2: Compare options
echo "${user}_backup" always prints _backup even if user is empty. echo "$user_backup" looks for variable user_backup. echo "${user}_backup" && [ -n "$user" ] has wrong syntax mixing echo and condition.
Final Answer:
echo "${user:+${user}_backup}" -> Option A
Quick Check:
Use ${var:+value} to print only if var set = B [OK]
Hint: Use ${var:+value} to print only if var is set [OK]
Common Mistakes:
Using $user_backup instead of ${user}_backup
Printing _backup even if user empty
Mixing echo with condition incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.