Bird
0
0

You want to create a bash script that prints the value of variable user followed by the string _backup only if user is not empty. Which is the best way to write the echo command?

hard🚀 Application Q15 of 15
Bash Scripting - Variables
You want to create a bash script that prints the value of variable user followed by the string _backup only if user is not empty. Which is the best way to write the echo command?
Aecho "${user:+${user}_backup}"
Becho "${user}_backup"
Cecho "${user}_backup" && [ -n "$user" ]
Decho "$user_backup"
Step-by-Step Solution
Solution:
  1. Step 1: Understand conditional variable expansion

    The expression ${user:+${user}_backup} prints ${user}_backup only if user is not empty.
  2. Step 2: Compare options

    echo "${user}_backup" always prints _backup even if user is empty. echo "$user_backup" looks for variable user_backup. echo "${user}_backup" && [ -n "$user" ] has wrong syntax mixing echo and condition.
  3. Final Answer:

    echo "${user:+${user}_backup}" -> Option A
  4. Quick Check:

    Use ${var:+value} to print only if var set = B [OK]
Quick Trick: Use ${var:+value} to print only if var is set [OK]
Common Mistakes:
MISTAKES
  • Using $user_backup instead of ${user}_backup
  • Printing _backup even if user empty
  • Mixing echo with condition incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes