Set Output of Command to Variable in Bash

Bash Set output of command to variable

Using Substitution Syntax

Use substitution syntax to capture the output of a single command into a variable in Bash.

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.

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.

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.

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.

Use backticks to capture the output of a pipeline into a variable in Bash.

Use backticks to capture the output of a multiline command to a variable in Bash.

Use backticks to capture the output of a for loop into a variable in Bash.

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.

Using read Command

Use the read command to capture the output of a single command into a variable in Bash.

Use the read command to capture the output of a pipeline into a variable in Bash.

Use the read command to capture the output of a multiline command to a variable in Bash.

Use the read command to capture the output of a for loop into a variable in Bash.

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *