0
0
Bash Scriptingscripting~10 mins

String variables in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String variables
Declare variable
Assign string value
Use variable
Print or manipulate string
End
This flow shows how a string variable is declared, assigned, used, and printed in bash scripting.
Execution Sample
Bash Scripting
name="Alice"
echo "Hello, $name!"
Assigns the string 'Alice' to variable 'name' and prints a greeting using that variable.
Execution Table
StepActionVariable 'name'Output
1Declare and assign name="Alice"Alice
2Print greeting with echoAliceHello, Alice!
3End of scriptAlice
💡 Script ends after printing the greeting.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
nameundefinedAliceAliceAlice
Key Moments - 2 Insights
Why do we use double quotes around the string when assigning or printing?
Double quotes allow the variable to expand and preserve spaces. Without quotes, the string might split or variables won't expand properly, as shown in step 2 of the execution_table.
What happens if we forget the $ before the variable name in echo?
If you write echo "Hello, name!" without $, it prints the word 'name' literally instead of the variable's value, unlike step 2 where $name expands to 'Alice'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 1, what is the value of variable 'name'?
AAlice
Bundefined
C"Hello"
Dempty string
💡 Hint
Check the 'Variable name' column at step 1 in execution_table.
At which step does the script print output to the screen?
AStep 1
BStep 2
CStep 3
DNo output is printed
💡 Hint
Look at the 'Output' column in execution_table.
If we remove the $ in echo "Hello, $name!", what will be printed?
AHello, Alice!
BHello, $name!
CHello, name!
DError message
💡 Hint
Refer to key_moments explanation about variable expansion.
Concept Snapshot
String variables in bash:
- Assign with: name="value"
- Use with $name
- Use double quotes to preserve spaces and expand variables
- Print with echo "Hello, $name!"
- Without $, variable name prints literally
Full Transcript
In bash scripting, string variables are created by assigning a value with an equals sign and no spaces, like name="Alice". To use the variable, prefix it with a dollar sign, for example, echo "Hello, $name!" prints Hello, Alice!. Double quotes are important to keep the string intact and allow variable expansion. Forgetting the $ prints the variable name literally. This simple flow helps beginners see how string variables work step-by-step.