uname -r prints the running kernel release string.
Save that output in LAB_KERNEL.
Which command is appropriate?
Command substitution $(...) is replaced by standard output. Combining it with an assignment stores the result in a shell variable.
Detailed explanation
LAB_KERNEL='uname -r'Incorrect. Single quotes store the literal text uname -r.
Single quotes make uname -r literal data. This option stores a command name as text without executing that command, so it does not obtain the kernel release.
uname -r > LAB_KERNELIncorrect. Redirection writes a file named LAB_KERNEL rather than assigning a variable.
LAB_KERNEL is a filename following >, not a variable assignment. Ordinary > can truncate an existing file, so compare this candidate only in a fresh temporary directory.
$(uname -r)Incorrect. A command substitution by itself is not an assignment.
There is no assignment word. The substituted output occupies command position and is used as a command name. The exact outcome depends on that name, but this form does not save a value in LAB_KERNEL.
LAB_KERNEL=$(uname -r)Correct. The command's output is substituted and assigned to LAB_KERNEL.
The standard output of uname -r becomes the assignment value with trailing newlines removed. An assignment value is not subject to ordinary word splitting or filename expansion. Quote "$LAB_KERNEL" when using the stored value later.
Try it yourself
An example you can run in a temporary verification environment.
LAB_KERNEL=$(uname -r)
if [[ -n "$LAB_KERNEL" && "$LAB_KERNEL" == "$(uname -r)" ]]; then printf 'release-match=yes\n'; else printf 'release-match=no\n'; fi
LAB_SAMPLE=$(printf 'line one\nline two\n\n')
printf 'sample=<%s>\n' "$LAB_SAMPLE"Official references
- Official documentation 1
- Official documentation 2
- Official documentation 3
- Official documentation 4
- Official documentation 5
Expected result
release-match=yes
sample=<line one
line two>Key points
- Command substitution
- Standard output
- Variable assignment
Notes
- Environment: Bash 5.2 / GNU coreutils uname. Compare the host-dependent release rather than assuming a fixed string. Output capture was reproduced with Bash 5.2.37 / GNU coreutils 8.32 in Git Bash.
- The release varies by host. The release-match line checks a nonempty stored value against uname -r instead of expecting a fixed release. The line one/line two sample is synthetic supporting data, not a real kernel release.
- Ordinary command substitution captures standard output. It does not automatically capture standard error. Use double quotes when reusing the resulting scalar value.
Foundation review
Trailing and embedded newlines
Ordinary $(...) removes all trailing newlines. Embedded newlines remain, so the verification quotes a supporting two-line sample that originally ends with two newline characters.
Assignment value and command position
The output has different uses when substitution is on the right of an assignment and when it stands alone in command position. Capturing output alone does not establish a named variable.
Files and variables are separate destinations
The filename after > and the variable name before = can have the same spelling while denoting different destinations. Read the syntax around LAB_KERNEL.