Bird
Raised Fist0
Bash Scriptingscripting~10 mins

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

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

Practice

(1/5)
1. What is the correct way to assign the string Hello to a variable named greeting in bash?
easy
A. greeting="Hello"
B. greeting = "Hello"
C. greeting: "Hello"
D. greeting <- "Hello"

Solution

  1. Step 1: Understand bash variable assignment syntax

    In bash, variable assignment must have no spaces around the equals sign.
  2. Step 2: Check the options for correct syntax

    greeting="Hello" uses no spaces and quotes the string correctly. The other options have invalid syntax.
  3. Final Answer:

    greeting="Hello" -> Option A
  4. Quick Check:

    No spaces around = and quotes needed [OK]
Hint: No spaces around = and use quotes for strings [OK]
Common Mistakes:
  • Adding spaces around = causes syntax errors
  • Forgetting quotes around strings with spaces
  • Using wrong assignment symbols like : or <-
2. Which of the following correctly prints the value of the variable name in bash?
easy
A. echo name
B. echo $name
C. echo &name
D. echo %name

Solution

  1. Step 1: Recall how to access variable values in bash

    Use the dollar sign ($) before the variable name to get its value.
  2. Step 2: Evaluate each option

    echo $name uses $name correctly. echo name prints the literal word 'name'. The other options use invalid symbols.
  3. Final Answer:

    echo $name -> Option B
  4. Quick Check:

    Use $ to access variable value [OK]
Hint: Use $ before variable to print its value [OK]
Common Mistakes:
  • Forgetting $ prints variable name, not value
  • Using wrong symbols like & or %
  • Confusing variable access with assignment
3. What will be the output of this bash script?
name="Alice"
echo "Hello, $name!"
medium
A. Hello, Alice!
B. Hello, $name!
C. Hello, name!
D. Hello, !

Solution

  1. Step 1: Understand variable assignment and usage

    The variable name is assigned the string "Alice".
  2. Step 2: Analyze the echo command with variable expansion

    The echo command uses double quotes, so $name is replaced by its value "Alice".
  3. Final Answer:

    Hello, Alice! -> Option A
  4. Quick Check:

    Variables expand inside double quotes [OK]
Hint: Variables expand inside double quotes, not single quotes [OK]
Common Mistakes:
  • Expecting variable expansion inside single quotes
  • Printing variable name literally without $
  • Forgetting to assign variable before use
4. Identify the error in this bash script:
message = "Hi there"
echo $message
medium
A. Variable name is invalid
B. Quotes should be single quotes
C. Missing $ before variable in echo
D. Spaces around = cause syntax error

Solution

  1. Step 1: Check variable assignment syntax

    In bash, no spaces are allowed around the equals sign when assigning variables.
  2. Step 2: Analyze the given script

    The line message = "Hi there" has spaces around =, causing a syntax error.
  3. Final Answer:

    Spaces around = cause syntax error -> Option D
  4. Quick Check:

    No spaces around = in assignments [OK]
Hint: No spaces around = in variable assignment [OK]
Common Mistakes:
  • Adding spaces around =
  • Using wrong quotes type causes no error here
  • Forgetting $ in echo prints variable name literally
5. You want to store a user's full name with spaces in a variable fullname and then print it. Which script correctly does this?
hard
A. fullname=John Doe echo $fullname
B. fullname='John Doe' echo fullname
C. fullname="John Doe" echo $fullname
D. fullname=John_Doe echo $fullname

Solution

  1. Step 1: Assign string with spaces to variable

    Strings with spaces must be quoted to be assigned correctly in bash.
  2. Step 2: Print the variable value

    Use echo $fullname to print the stored string value.
  3. Final Answer:

    fullname="John Doe" echo $fullname -> Option C
  4. Quick Check:

    Quote strings with spaces and use $ to print [OK]
Hint: Quote strings with spaces; use $ to print variable [OK]
Common Mistakes:
  • Not quoting strings with spaces causes errors
  • Printing variable name without $ prints literal text
  • Using underscores instead of spaces changes the string
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