Command substitution allows you to capture the output of a command and use it as part of another command or store it in a variable.
#!/bin/bash
current_date=$(date +"%Y-%m-%d")
echo "Current date: $current_date"
Here, $(date +"%Y-%m-%d")
captures the output of the date
command and assigns it to the current_date
variable.
Alternatively, you can use backticks for command substitution. However, it's recommended to use the $()
syntax for better readability and nesting.
#!/bin/bash
files_count=`ls | wc -l`
echo "Number of files: $files_count"
To capture multi-line output, use double quotes around the command substitution.
#!/bin/bash
file_list="$(ls -l)"
echo -e "File List:\n$file_list"
The double quotes preserve the line breaks in the output, allowing for proper formatting.
You can redirect command output directly to a variable using the command substitution syntax.
#!/bin/bash
file_contents=$(cat example.txt)
echo "File Contents:\n$file_contents"
Here, the cat
command output (contents of "example.txt") is stored in the file_contents
variable.
Capture command output to make decisions in conditional statements.
#!/bin/bash
if result=$(command_with_output); then
echo "Command succeeded. Output: $result"
else
echo "Command failed."
fi
Here, the output of command_with_output
is captured, and the script responds based on the success or failure of the command.
Pass the output of one command as an argument to another.
#!/bin/bash
directory=$(pwd)
echo "Current directory: $directory"
# Using the output as an argument
files_count=$(ls "$directory" | wc -l)
echo "Number of files in $directory: $files_count"
Iterate over command output using a loop.
#!/bin/bash
for file in $(ls); do
echo "Processing file: $file"
# Additional processing logic
done
This loop iterates over the files in the current directory using the output of the ls
command.
Capture both standard output (stdout) and standard error (stderr) for comprehensive error handling.
#!/bin/bash
result=$(command 2>&1)
if [ $? -eq 0 ]; then
echo "Command succeeded. Output: $result"
else
echo "Command failed. Error: $result"
fi
Here, 2>&1
redirects stderr to stdout, and the exit code is checked for error handling.
When capturing command output from user input or external sources, validate and sanitize inputs to prevent command injection vulnerabilities.
#!/bin/bash
user_input="; echo 'Dangerous command';"
echo "User input: $user_input"
# Sanitize and validate input before using it
output=$(echo "$user_input" | tr -cd '[:alnum:]')
echo "Processed input: $output"
When using pipes (|
) in commands, you can capture and use intermediate results in variables.
#!/bin/bash
files_with_txt_extension=$(ls | grep ".txt")
echo "Files with .txt extension: $files_with_txt_extension"
Here, the output of ls
is piped to grep
to filter files with a ".txt" extension, and the result is stored in the files_with_txt_extension
variable.
Capturing command output is a powerful feature in Linux scripting, allowing you to build flexible and dynamic scripts that respond to the results of executed commands. Always consider error handling and security when working with command output in scripts.