LPIC-1 Shells, Scripting and Data Management Practice Question
Which TWO of the following are valid ways to capture the output of a command into a variable in Bash?
⚠ Common exam trap
LPI often tests the distinction between command substitution syntax and other shell constructs like brace expansion or simple assignment, leading candidates to confuse var=$(command) with var={command} or var=command.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
var=`command`
Option A, var=`command`, is correct because backticks are the legacy command-substitution syntax in Bash: the shell runs command in a subshell and replaces the backtick expression with its standard output, which is then assigned to var. Option C, var=$(command), is correct because the modern $(...) form performs the same command substitution, capturing command's stdout into var, and is preferred since it nests more cleanly and avoids backtick escaping issues. Option B, var={command}, is not valid because braces are used for brace expansion (e.g., {a,b}) and parameter expansion (${var}), not command substitution. Option D, var|command, is not valid because it would attempt to pipe the variable name as a command into another command rather than assign output. Option E, var=command, is not valid because it simply assigns the literal string 'command' to var without executing it or capturing any output.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
var=`command`
Why this is correct
Backticks perform command substitution in Bash, executing the enclosed command and assigning its standard output to the variable. This is a valid capture method, equivalent in effect to the modern $(command) syntax, and works in all POSIX-compliant shells.
- ✗
var={command}
Why it's wrong here
Braces perform brace expansion, not command substitution, so `var={command}` assigns the literal text. Command substitution requires `$(command)` or backticks. Brace expansion is genuinely useful for generating lists, such as `touch file{1..3}` or `cp a.txt{,.bak}`, but it never executes the enclosed text as a command.
- ✓
var=$(command)
Why this is correct
Command substitution with $(...) runs the command in a subshell and substitutes its standard output in place, assigning the result to var. This is the modern, nestable syntax that satisfies the stem's requirement for capturing command output into a variable.
- ✗
var|command
Why it's wrong here
A pipe sends command output to another process's stdin; it never assigns anything to a variable, and placing var before the pipe is a syntax error. It tempts because pipes capture output streams, but assignment requires command substitution syntax instead.
- ✗
var=command
Why it's wrong here
Without backticks or $(), Bash reads var=command as a literal string assignment, storing the text "command" rather than its output. It tempts because it resembles the assignment form, but command substitution syntax is mandatory to execute and capture.
Visual reference
Go deeper
Related to this question
About these practice questions
Courseiva writes every LPIC-1 question from scratch — 402 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This LPIC-1 practice question is part of Courseiva's free LPI certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the LPIC-1 exam.