0
0
Bash Scriptingscripting~10 mins

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

Choose your learning style9 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.