How to Set Variable to Output of Command in Bash

Fumbani Banda Feb 02, 2024
  1. Command Substitution in Bash
  2. Bash Command Output to Variable
How to Set Variable to Output of Command in Bash

This tutorial demonstrates assigning the output of a command to a variable in bash using command substitution.

Command Substitution in Bash

Command substitution is a bash feature that enables us to run Linux commands and store the command’s output in a bash variable. Once a command is executed using the command substitution syntax, the command’s standard output replaces the command, with any trailing newlines removed.

Command substitution uses two different syntaxes to store the command output to a variable.

The first syntax type puts the command inside the parenthesis and adds a $ sign at the beginning, as shown below.

Every character inside the parenthesis is treated as part of the command.

output=$(command)
output=$(command argument-1)
output=$(/path/to/command)
output=$(/path/to/command argument-1)

The second type of syntax uses backticks around the command.

output=`command`
output=`command argument-1`
output=`/path/to/command`
output=`/path/to/command argument-1`

Bash Command Output to Variable

We use the following examples to demonstrate command substitution in a bash script.

Below, we use the first syntax of parenthesis and a dollar sign at the beginning. The whoami command is executed, and the output replaces the command, which is then assigned to the user variable. We use the echo command to display the message to standard output.

user=$(whoami)
echo "The logged in user is $user"

Running this script displays the following output.

The logged in user is delftstack

Below, we use the backticks syntax. The output of the whoami command is assigned to the user variable, and echo is used to print the message to the terminal.

user=`whoami`
echo "The logged in user is $user"

Running the script displays the output below.

The logged in user is delftstack

In the script below, we use ls to list the contents of the delftstack folder using the parenthesis and the dollar sign syntax. The output of the ls command is assigned to the files variable. The echo command is used to print out the content of the files variable to the terminal.

files=$(ls -l delftstack)
echo "$files"

Running the script prints the following output to the standard output.

total 4
-rw-r--r-- 1 fumba fumba  752 Nov 17 12:51 directory.sh
-rw-r--r-- 1 fumba fumba   79 Nov 19 13:11 foo1.sh
drwxr-xr-x 1 fumba fumba 4096 Nov 22 13:57 part
-rw-r--r-- 1 fumba fumba  213 Nov 21 21:10 regex.sh

The below bash script uses the backticks syntax to assign the output of the ls command to the files variable, and the echo command is used to print the content of the files variable to the standard output.

files=`ls -l delftstack`
echo "$files"

Running the script displays the following output.

total 4
-rw-r--r-- 1 fumba fumba  752 Nov 17 12:51 directory.sh
-rw-r--r-- 1 fumba fumba   79 Nov 19 13:11 foo1.sh
drwxr-xr-x 1 fumba fumba 4096 Nov 22 13:57 part
-rw-r--r-- 1 fumba fumba  213 Nov 21 21:10 regex.sh

We use the following syntax to assign a multiline command to a variable in the bash script.

variable_name=$(command \ 
argument 1 \ 
argument 2 \ 
argument 3)

The bash script below demonstrates assigning the output of a multiline command to a variable. The ls command list all the contents of the current folder, and the output is piped to the head command. The head command only selects the first 5 lines of the input from ls and pipes the output to the grep command. The grep command matches the line with the word flow and prints it out to the standard output.

files=$(ls -l \
| \
head -5 \
| grep -i "flow")

echo $files

Running the script displays the following output.

drwxr-xr-x 1 fumba fumba 4096 Sep 30 09:29 airflow
Fumbani Banda avatar Fumbani Banda avatar

Fumbani is a tech enthusiast. He enjoys writing on Linux and Python as well as contributing to open-source projects.

LinkedIn GitHub

Related Article - Bash Output