Bird
0
0

What is wrong with this bash script snippet?

medium📝 Debug Q6 of 15
Bash Scripting - File Operations in Scripts
What is wrong with this bash script snippet?
tempfile=mktemp
echo $tempfile
AThe script lacks <code>#!/bin/bash</code> shebang
BThe variable <code>tempfile</code> is not declared
CThe <code>echo</code> command is missing quotes
DThe command <code>mktemp</code> is not executed; it is assigned as a string
Step-by-Step Solution
Solution:
  1. Step 1: Check variable assignment

    Assigning tempfile=mktemp sets the string 'mktemp' to the variable, not the output of the command.
  2. Step 2: Correct usage

    Use command substitution: tempfile=$(mktemp) to execute and assign the output.
  3. Step 3: Other options

    The variable tempfile is not declared is incorrect because variables do not require declaration. The echo command is missing quotes is not an error; quotes are optional here. The script lacks #!/bin/bash shebang is unrelated to the error.
  4. Final Answer:

    The command mktemp is not executed; it is assigned as a string -> Option D
  5. Quick Check:

    Is command substitution missing? Yes. [OK]
Quick Trick: Use $(mktemp) to execute command, not plain assignment [OK]
Common Mistakes:
MISTAKES
  • Assigning command name as string instead of executing
  • Confusing variable declaration with execution

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes