Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Accessing variables ($var and ${var}) in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Accessing variables ($var and ${var})
Define variable var=World
↓
Access with $var
→Print value
↓
Access with ${var}
→Print value
↓
Use in strings or with suffixes
↓
Output final combined string
This flow shows how a variable is defined and accessed using $var and ${var}, including usage in strings.
Execution Sample
Bash Scripting
var=World

echo Hello $var

echo Hello ${var}!

name=User

echo Welcome ${name}123
This script defines variables and prints them using $var and ${var} syntax, showing how to append text safely.
Execution Table
StepCommandVariable AccessOutput
1var=WorldSet var
2echo Hello $var$varHello World
3echo Hello ${var}!${var}Hello World!
4name=UserSet name
5echo Welcome ${name}123${name} with suffixWelcome User123
6echo Welcome $name123$name123 (undefined)Welcome
💡 Script ends after printing all lines; last echo shows difference between $var and ${var} with suffix.
Variable Tracker
VariableStartAfter Step 1After Step 4Final
varundefinedWorldWorldWorld
nameundefinedundefinedUserUser
Key Moments - 2 Insights
Why do we use ${var} instead of $var when adding text right after the variable?
Using ${var} clearly marks the variable name boundary, so the shell knows where the variable ends and the text begins, as shown in step 5 vs step 6 in the execution_table.
What happens if we write $name123 instead of ${name}123?
The shell looks for a variable named 'name123' which is undefined, so it prints nothing for that part, as seen in step 6 output.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of step 3?
AHello World
BHello World!
CHello ${var}!
DHello !
💡 Hint
Check the Output column for step 3 in the execution_table.
At which step is the variable 'name' first set?
AStep 4
BStep 2
CStep 1
DStep 5
💡 Hint
Look at the Command and Variable Access columns to see when 'name' is assigned.
If we change echo Welcome $name123 to echo Welcome ${name}123, what changes in output?
AOutput becomes 'Welcome '
BOutput becomes 'Welcome ${name}123'
COutput becomes 'Welcome User123'
DNo change in output
💡 Hint
Compare outputs of step 5 and step 6 in the execution_table.
Concept Snapshot
Accessing variables in bash:
- Use $var to get variable value.
- Use ${var} to clearly mark variable name.
- ${var} helps when adding text right after variable.
- Without braces, shell may misinterpret variable name.
- Always use braces to avoid confusion.
Full Transcript
This lesson shows how to access variables in bash scripts using $var and ${var}. We start by defining variables 'var' and 'name'. Then we print them using both $var and ${var} syntax. Using ${var} is important when you want to add text immediately after the variable name, so the shell knows where the variable ends. For example, echo Hello ${var}! prints 'Hello World!' while echo Hello $var! also works here because the exclamation mark is separate. But echo Welcome $name123 tries to find a variable named 'name123' which is not set, so it prints nothing after 'Welcome '. Using echo Welcome ${name}123 correctly prints 'Welcome User123'. This helps avoid confusion and errors in scripts.

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

  1. Step 1: Understand variable access syntax

    In bash, $var accesses the value of a variable named var.
  2. Step 2: Purpose of braces

    Using ${var} helps separate the variable name from other text, avoiding confusion when concatenating.
  3. Final Answer:

    To clearly separate the variable name from surrounding text -> Option A
  4. 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

  1. Step 1: Understand variable and string concatenation

    To print variable name followed by _end, we must separate the variable name from the string.
  2. 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.
  3. Final Answer:

    echo ${name}_end -> Option D
  4. Quick Check:

    Braces separate variable name from text = C [OK]
Hint: Use braces when adding text right after variable [OK]
Common Mistakes:
  • Forgetting braces and getting wrong variable
  • Adding space breaks concatenation
  • Using wrong variable name inside braces
3. What will be the output of this bash script?
var="world"
echo "Hello $var!"
echo "Hello ${var}!"
echo "Hello $var123"
echo "Hello ${var}123"
medium
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

  1. 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".
  2. Step 2: Combine outputs

    Outputs are:
    Hello world!
    Hello world!
    Hello
    Hello world123
  3. Final Answer:

    Hello world! Hello world! Hello Hello world123 -> Option B
  4. 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

  1. Step 1: Understand variable expansion with suffix

    Using $filename.txt makes bash look for variable named filename.txt, which does not exist.
  2. Step 2: Fix with braces

    Using ${filename}.txt correctly expands filename and appends .txt.
  3. Final Answer:

    Because $filename.txt looks for variable named filename.txt which is undefined -> Option C
  4. 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

  1. Step 1: Understand conditional variable expansion

    The expression ${user:+${user}_backup} prints ${user}_backup only if user is not empty.
  2. 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.
  3. Final Answer:

    echo "${user:+${user}_backup}" -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard