Bird
0
0

You want to create a script that prints a message with a variable count inside double quotes, but only if count is not empty. Which is the best way to write this?

hard🚀 Application Q15 of 15
Bash Scripting - Quoting and Expansion
You want to create a script that prints a message with a variable count inside double quotes, but only if count is not empty. Which is the best way to write this?
Aif [ $count ]; then echo "Count is $count"; fi
Bif [ $count ]; then echo 'Count is $count'; fi
Cif [ "$count" ]; then echo "Count is $count"; fi
Dif [ "$count" ]; then echo 'Count is $count'; fi
Step-by-Step Solution
Solution:
  1. Step 1: Check condition with quotes

    Using [ "$count" ] safely tests if count is non-empty, even if it contains spaces or is unset.
  2. Step 2: Print message with double quotes

    Using double quotes in echo expands $count properly inside the string.
  3. Final Answer:

    if [ "$count" ]; then echo "Count is $count"; fi -> Option C
  4. Quick Check:

    Quote variables in tests and output for safety = D [OK]
Quick Trick: Quote variables in tests and output to avoid errors [OK]
Common Mistakes:
MISTAKES
  • Not quoting variable in test causing errors
  • Using single quotes in echo preventing expansion
  • Testing variable without quotes causing syntax errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes