Table of Contents
Using Substitution Syntax
Use substitution syntax to capture the output of a single command into a variable in Bash.
1 2 3 4 |
date_variable=$(date) echo "Today's date: $date_variable" |
1 2 3 |
Today's date: Thu Jun 8 04:08:47 UTC 2023 |
We used the date
command to get the current date and time. The date
command was enclosed within the substitution syntax represented by the $(...)
, which we used to capture the output of the date
command. After that, we stored the captured output in a variable named date_variable
. Finally, we used an echo
command to display the value of the date_variable
with a custom string on the Bash console.
Use substitution syntax to capture the output of a pipeline into a variable in Bash.
1 2 3 4 |
file_count=$(ls | wc -l) echo "$file_count" |
1 2 3 |
7 |
In the above example, we used substitution syntax ($(...)
) to grab the pipeline output. The pipeline included two commands: ls
and wc -l
.
The ls
listed all files and directories in the current directory; this command generated the list of directories and file names as output. Note that the substitution syntax will not capture the output of the ls
command, but it will capture the final result produced by the pipeline.
So, the output of the ls
command was passed to the wc -l
command using a pipe (|
). The wc
command counts the received input’s lines, words, and characters. In the above code fence, we used the -l
option to count the lines in the received input.
In the pipeline, the previous command’s output is the next command’s input.
Now, the substitution syntax ($(...)
) captured the output of the ls | wc -l
pipeline and stored it in the file_count
variable, which we further used with an echo
command to display its value on the Bash console.
Use substitution syntax to capture the output of a multiline command to a variable in Bash.
1 2 3 4 5 6 7 |
string_value=$(echo "This is line one \ This is line two. \ This is line three.\ This is line four.") echo "$string_value" |
1 2 3 |
This is line one This is line two. This is line three.This is line four. |
Again, we used the $(...)
to capture the command output. It is similar to the first example, but the important point is writing a multiline command by enclosing them in the substitution syntax. To write the multiline command, you only need to write \
before moving to a new line (see the above example).
Use substitution syntax to capture the output of a for
loop into a variable in Bash.
1 2 3 4 5 6 7 |
for_loop_output=$(for i in {1..5}; do \ echo "This is Iteration: $i"; \ echo "Now Processing File $i.txt"; \ done) echo "$for_loop_output" |
1 2 3 4 5 6 7 8 9 10 11 12 |
This is Iteration: 1 Now Processing File 1.txt This is Iteration: 2 Now Processing File 2.txt This is Iteration: 3 Now Processing File 3.txt This is Iteration: 4 Now Processing File 4.txt This is Iteration: 5 Now Processing File 5.txt |
This example resembles the previous code snippet. As the for
loop is a multiline process, we wrote a backslash (\
) before moving to the new line. Then, we enclosed the entire for
loop into the substitution syntax to capture its output and stored it into a variable called for_loop_output
. Finally, we used an echo
command to print its value on the Bash console.
Ensure you don’t have a space before and after the equal sign.
Using Backticks
Use backticks to capture the output of a single command into a variable in Bash.
1 2 3 4 |
date_variable=`date` echo "Today's date: $date_variable" |
1 2 3 |
Today's date: Thu Jun 8 04:46:06 UTC 2023 |
Use backticks to capture the output of a pipeline into a variable in Bash.
1 2 3 4 |
file_count=`ls | wc -l` echo "$file_count" |
1 2 3 |
7 |
Use backticks to capture the output of a multiline command to a variable in Bash.
1 2 3 4 5 6 7 |
string_value=`echo "This is line one \ This is line two. \ This is line three.\ This is line four."` echo "$string_value" |
1 2 3 |
This is line one This is line two. This is line three.This is line four. |
Use backticks to capture the output of a for
loop into a variable in Bash.
1 2 3 4 5 6 7 |
for_loop_output=`for i in {1..5}; do \ echo "This is Iteration: $i"; \ echo "Now Processing File $i.txt"; \ done` echo "$for_loop_output" |
1 2 3 4 5 6 7 8 9 10 11 12 |
This is Iteration: 1 Now Processing File 1.txt This is Iteration: 2 Now Processing File 2.txt This is Iteration: 3 Now Processing File 3.txt This is Iteration: 4 Now Processing File 4.txt This is Iteration: 5 Now Processing File 5.txt |
All the code snippets are the same as we learned while using substitution syntax. The only difference is that we enclosed the commands within the backticks rather than the substitution syntax; see the above examples carefully.
Ensure you don’t have a space before and after the equal sign while using backticks to capture the command’s output.
Further reading:
Using read
Command
Use the read
command to capture the output of a single command into a variable in Bash.
1 2 3 4 |
date | read date_variable echo "Today's date: $date_variable" |
1 2 3 |
Today's date: Thu Jun 8 04:46:06 UTC 2023 |
Use the read
command to capture the output of a pipeline into a variable in Bash.
1 2 3 4 |
ls | wc -l | read file_count echo "$file_count" |
1 2 3 |
7 |
Use the read
command to capture the output of a multiline command to a variable in Bash.
1 2 3 4 5 6 7 |
echo "This is line one \ This is line two. \ This is line three.\ This is line four." | read string_value echo "$string_value" |
1 2 3 |
This is line one This is line two. This is line three.This is line four. |
Use the read
command to capture the output of a for
loop into a variable in Bash.
1 2 3 4 5 6 7 |
for i in {1..5}; do \ echo "This is Iteration: $i"; \ echo "Now Processing File $i.txt"; \ done | read for_loop_output echo "$for_loop_output" |
1 2 3 4 5 6 7 8 9 10 11 12 |
This is Iteration: 1 Now Processing File 1.txt This is Iteration: 2 Now Processing File 2.txt This is Iteration: 3 Now Processing File 3.txt This is Iteration: 4 Now Processing File 4.txt This is Iteration: 5 Now Processing File 5.txt |
The code snippets used in this section are the same as we used in the last two sections while using substitution syntax and backticks. This time, we did not enclose the commands within the backticks or substitution syntax, whether single-line or multiline commands, but we piped the output of these commands to the read
command.
The read
command reads the input received from the previous process and stores it in the specified variable. See the examples above to clarify the concept.
You must understand how pipelines work to use the
read
command.
That’s all about how to set output of command to variable in Bash.