Capture hostname's standard output at execution time.
Assign the result to NODE_NAME.
Which Bash assignment is appropriate?
$(command) is command substitution. It expands to the command's standard output with trailing newlines removed.
Detailed explanation
NODE_NAME='hostname'Incorrect. Single quotes preserve the literal text hostname.
Incorrect. Single quotes preserve the literal text hostname.
NODE_NAME=hostnameIncorrect. An unquoted word is assigned as a literal value.
Incorrect. An unquoted word is assigned as a literal value.
NODE_NAME=$(hostname)Correct. NODE_NAME=$(hostname) executes hostname and assigns its output.
Correct. NODE_NAME=$(hostname) executes hostname and assigns its output.
NODE_NAME=${hostname}Incorrect. ${hostname} expands a variable named hostname.
Incorrect. ${hostname} expands a variable named hostname.
Try it yourself
An example you can run in a temporary verification environment.
bash --noprofile --norc -c 'NODE_NAME=$(printf "node-a\n"); printf "<%s>\n" "$NODE_NAME"'Expected result
<node-a>Key points
- $() performs command substitution
- It expands standard output
- Trailing newlines are removed
Notes
- Environment: GNU Bash 5.2 / 状態変更なし
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.