🐧 Linux / Shell Scripting
Intermediate
What is command substitution in bash?
Answer
Command substitution captures the output of a command and inserts it into another command or variable assignment. Modern syntax: $(command) — preferred. Legacy syntax: `command` (backticks) — avoid, as backticks don't nest well. Examples: today=$(date +%Y-%m-%d) stores the current date. files=$(ls *.txt) stores filenames. echo "You are $(whoami)". Nesting: $(outer $(inner)). The output has trailing newlines stripped. Be careful with filenames containing spaces — always quote: file="$(find . -name '*.conf' | head -1)". Command substitution creates a subshell, so variable changes inside don't affect the parent shell.