What if you could change one word and update your whole script instantly?
Why Accessing variables ($var and ${var}) in Bash Scripting? - Purpose & Use Cases
Imagine you have a list of names and you want to greet each person by typing their name manually every time in your script.
You write: echo Hello Alice, then echo Hello Bob, and so on for every name.
This manual way is slow and boring. If you want to change the greeting or add more names, you must rewrite many lines.
It's easy to make mistakes, like misspelling a name or forgetting to update all lines.
Using variables like $var or ${var} lets you store a value once and reuse it everywhere.
This saves time, reduces errors, and makes your script flexible and easy to update.
echo Hello Alice echo Hello Bob
name=Alice
echo Hello $name
name=Bob
echo Hello ${name}You can write scripts that adapt quickly by changing values in one place, making automation smooth and error-free.
Think about a backup script where you set the folder path once in a variable, then use it many times to copy files without typing the path repeatedly.
Variables let you store and reuse information easily.
Using $var or ${var} makes scripts cleaner and less error-prone.
Changing a variable updates all places where it's used, saving time.
Practice
${var} instead of just $var?Solution
Step 1: Understand variable access syntax
In bash,$varaccesses 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 AQuick Check:
Braces separate variable name from text = A [OK]
- Thinking braces declare variables
- Using braces to comment variables
- Assuming braces change variable case
name followed immediately by the string _end in bash?Solution
Step 1: Understand variable and string concatenation
To print variablenamefollowed by_end, we must separate the variable name from the string.Step 2: Use braces to separate variable name
Using${name}_endcorrectly prints the variable value plus the string_end. Without braces,$name_endlooks for a variable namedname_end.Final Answer:
echo ${name}_end -> Option DQuick Check:
Braces separate variable name from text = C [OK]
- Forgetting braces and getting wrong variable
- Adding space breaks concatenation
- Using wrong variable name inside braces
var="world"
echo "Hello $var!"
echo "Hello ${var}!"
echo "Hello $var123"
echo "Hello ${var}123"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".Step 2: Combine outputs
Outputs are:
Hello world!
Hello world!
Hello
Hello world123Final Answer:
Hello world! Hello world! Hello Hello world123 -> Option BQuick Check:
Braces separate variable name correctly = D [OK]
- Assuming $var123 expands var plus 123
- Ignoring empty variable var123
- Confusing output lines order
filename="report" echo "File: $filename.txt"
Why does it not print
File: report.txt as expected?Solution
Step 1: Understand variable expansion with suffix
Using$filename.txtmakes bash look for variable namedfilename.txt, which does not exist.Step 2: Fix with braces
Using${filename}.txtcorrectly expandsfilenameand appends.txt.Final Answer:
Because $filename.txt looks for variable named filename.txt which is undefined -> Option CQuick Check:
Braces needed to separate variable name from suffix = A [OK]
- Thinking dot is part of variable name
- Assuming echo can't print dots
- Ignoring variable name rules
user followed by the string _backup only if user is not empty. Which is the best way to write the echo command?Solution
Step 1: Understand conditional variable expansion
The expression${user:+${user}_backup}prints${user}_backuponly ifuseris not empty.Step 2: Compare options
echo "${user}_backup" always prints_backupeven ifuseris empty. echo "$user_backup" looks for variableuser_backup. echo "${user}_backup" && [ -n "$user" ] has wrong syntax mixing echo and condition.Final Answer:
echo "${user:+${user}_backup}" -> Option AQuick Check:
Use ${var:+value} to print only if var set = B [OK]
- Using $user_backup instead of ${user}_backup
- Printing _backup even if user empty
- Mixing echo with condition incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
