Problem Statement
What is the difference between $(command) and `command`?
Explanation
Command substitution executes command and replaces it with its output. Both $(command) and `command` work but $(command) is preferred modern syntax. Example: DATE=$(date +%Y-%m-%d) assigns current date to variable, FILES=$(ls *.txt) assigns file list. Use quotes to preserve newlines: "$(command)" keeps line breaks.
Advantages of $(command): easily nestable $(command1 $(command2)), more readable especially with complex expressions, works consistently across shells. Backticks `command` are legacy syntax, harder to nest (requires escaping: \`command\`), less readable in complex expressions. Both execute in subshell, so variable assignments inside don't affect parent shell.
Common uses: TODAY=$(date +%Y-%m-%d), COUNT=$(wc -l < file), IP=$(hostname -I | awk '{print $1}'). Store command output: OUTPUT=$(command 2>&1) captures both stdout and stderr. Inline usage: echo "There are $(ls | wc -l) files". Understanding command substitution enables dynamic scripts that adapt to system state and command output.
