Bird
0
0

What will be the output of this bash script?

medium📝 Command Output Q13 of 15
Bash Scripting - Variables
What will be the output of this bash script?
var="world"
echo "Hello $var!"
echo "Hello ${var}!"
echo "Hello $var123"
echo "Hello ${var}123"
AHello world! Hello world! Hello world123 Hello 123
BHello world! Hello world! Hello Hello world123
CHello world! Hello world! Hello Hello 123
DHello world! Hello world! Hello world123 Hello world123
Step-by-Step Solution
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]
Quick Trick: Braces avoid variable name confusion with suffixes [OK]
Common Mistakes:
MISTAKES
  • Assuming $var123 expands var plus 123
  • Ignoring empty variable var123
  • Confusing output lines order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes