Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Command substitution in Bash allows you to capture the output of a command and store it in a variable or use it directly within another command. It’s a fundamental technique that enables flexible scripting and dynamic operations. By substituting the output of one command into another, you can save time and resources, avoid intermediate files, and write concise, powerful scripts.
Command substitution is a core Bash feature that makes scripts more dynamic and concise. By substituting command outputs directly into variables or other commands, you can streamline file handling, date processing, and more without creating intermediate files. While both backticks and $( ) work, prefer $( ) for readability and easier nesting. Remember to pay attention to quoting, trailing newlines, and exit codes to avoid subtle bugs. Mastering command substitution will enable you to write more efficient and maintainable Bash scripts.
$( )
SyntaxThis is the preferred modern syntax for command substitution because it’s more readable and nestable.
RESULT=$(date +%Y-%m-%d)
echo "Today's date is: $RESULT"
`
)Bash also supports an older syntax using backticks:
RESULT=<code>date +%Y-%m-%d</code>
echo "Today's date is: $RESULT"
While both methods work, $( ) is generally preferred because:
CURRENT_DIR=$(pwd)
echo "We are in directory: $CURRENT_DIR"
You can then use $CURRENT_DIR for further operations such as file checks or path manipulations.
echo "We are in the $(pwd) directory on $(date +%A)."
This line will first execute pwd
and date +%A
commands and then embed the results in the output string.
Nesting with backticks can be tricky, since you need to escape inner backticks. But nesting with $( ) is simpler:
RESULT=$(echo "$(ls -l)" | wc -l)
echo "Number of lines in ls -l output: $RESULT"
In this example:
ls -l
runs first.ls -l
is passed to echo
(through $( ) nesting).wc -l
counts how many lines are in that output.Command substitution can capture multiple lines of output into a single variable:
ALL_FILES=$(ls)
echo "Files in current directory:"
echo "$ALL_FILES"
The variable ALL_FILES now contains the entire multi-line output of ls
. When echoing, use quotes to preserve line breaks.
Command substitution captures stdout, not the exit code. If you need the exit code of a command, store it directly or check $? right after the command:
OUTPUT=$(some_command)
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo "Command succeeded. Output: $OUTPUT"
else
echo "Command failed with exit code $RETVAL"
fi
echo "$OUTPUT"
rather than echo $OUTPUT
.$( )
to avoid confusion.Command substitution is usually used in the console itself, but it can also be one of the most powerful tools when creating scripts. You can learn more about Bash scripting in the following links: