Skip to main content
ubuntuask.com

Back to all posts

How to Assign Different Output to Different Variables In Bash?

Published on
4 min read
How to Assign Different Output to Different Variables In Bash? image

Best Bash Scripting Guides to Buy in October 2025

1 Black Hat Bash: Creative Scripting for Hackers and Pentesters

Black Hat Bash: Creative Scripting for Hackers and Pentesters

BUY & SAVE
$40.39 $59.99
Save 33%
Black Hat Bash: Creative Scripting for Hackers and Pentesters
2 The Ultimate Linux Shell Scripting Guide: Automate, Optimize, and Empower tasks with Linux Shell Scripting

The Ultimate Linux Shell Scripting Guide: Automate, Optimize, and Empower tasks with Linux Shell Scripting

BUY & SAVE
$27.89 $49.99
Save 44%
The Ultimate Linux Shell Scripting Guide: Automate, Optimize, and Empower tasks with Linux Shell Scripting
3 Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))

Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))

  • AFFORDABLE PRICES COMPARED TO NEW BOOKS-GREAT FOR BUDGET SHOPPERS!
  • QUALITY ASSURANCE: INSPECTED FOR GOOD CONDITION AND READABILITY.
  • ECO-FRIENDLY CHOICE: PROMOTE REUSE AND SUSTAINABILITY WITH EVERY PURCHASE.
BUY & SAVE
$22.15 $44.99
Save 51%
Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))
4 Classic Shell Scripting

Classic Shell Scripting

BUY & SAVE
$25.73 $49.99
Save 49%
Classic Shell Scripting
5 The Linux Command Line, 2nd Edition: A Complete Introduction

The Linux Command Line, 2nd Edition: A Complete Introduction

BUY & SAVE
$19.58 $39.99
Save 51%
The Linux Command Line, 2nd Edition: A Complete Introduction
6 Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

BUY & SAVE
$36.76 $49.95
Save 26%
Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)
7 The Book of Batch Scripting: From Fundamentals to Advanced Automation

The Book of Batch Scripting: From Fundamentals to Advanced Automation

BUY & SAVE
$59.99
The Book of Batch Scripting: From Fundamentals to Advanced Automation
8 Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali

Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali

BUY & SAVE
$23.74 $39.99
Save 41%
Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali
9 Bash Pocket Reference: Help for Power Users and Sys Admins

Bash Pocket Reference: Help for Power Users and Sys Admins

BUY & SAVE
$17.60 $24.99
Save 30%
Bash Pocket Reference: Help for Power Users and Sys Admins
+
ONE MORE?

In Bash, you can assign different outputs to different variables by using command substitution along with the assignment operator. Command substitution allows you to capture the output generated by a command and assign it to a variable.

To assign different outputs to different variables, you can use command substitution within a variable assignment statement. For example, you can assign the output of a command to a variable by enclosing the command within $() or backticks (`).

Here is an example of assigning different outputs to different variables in Bash:

variable1=$(command1) variable2=$(command2)

In the above example, command1 and command2 are separate commands whose outputs are captured and assigned to variable1 and variable2 respectively.

By using command substitution in this way, you can assign different outputs to different variables in your Bash scripts. This can be useful when you need to capture and store the output of multiple commands for further processing or manipulation in your scripts.

How to capture output of a script in a variable in bash?

You can capture the output of a script in a variable in bash by using command substitution. Here is an example:

output=$(./your_script.sh)

In this example, the $(...) syntax is used to capture the output of the script your_script.sh and store it in the variable output. You can then use the variable output to access the output of the script.

What is the syntax for assigning output to a variable in bash?

To assign the output of a command to a variable in bash, you can use the following syntax:

variable=$(command)

For example, if you want to store the output of the date command in a variable named current_date, you would use the following command:

current_date=$(date)

You can then use the variable current_date to access the output of the date command.

How to save command output to multiple variables in bash?

To save command output to multiple variables in bash, you can use the following syntax:

read var1 var2 var3 <<< "$(command)"

For example, if you want to save the output of the ls command to three different variables, you can do the following:

read var1 var2 var3 <<< "$(ls)"

This will save the output of the ls command into var1, var2, and var3 respectively. You can adjust the number of variables based on the output of the command.

How to store multiple outputs of a command in multiple variables in bash?

You can use command substitution to store the output of a command in multiple variables. Here is an example of how you can do this:

output=$(command) var1=$(echo "$output" | awk '{ print $1 }') var2=$(echo "$output" | awk '{ print $2 }') var3=$(echo "$output" | awk '{ print $3 }')

In this example, command is the command whose output you want to store in multiple variables. The output=$(command) syntax stores the output of the command in the output variable. Then, you can use awk or any other text processing tool to extract specific parts of the output and store them in different variables.

How to assign values of multiple commands to multiple variables in bash?

To assign values of multiple commands to multiple variables in bash, you can use command substitution by enclosing each command in $( ) and separate the commands with a semicolon. Here's an example:

# Assign output of each command to a separate variable var1=$(command1); var2=$(command2); var3=$(command3)

Alternatively, you can also use a single line to assign values to multiple variables using a single command substitution with a single command and then use the read command to assign each value to a variable. Here's an example:

# Assign output of a single command to multiple variables read var1 var2 var3 <<< $(command)

Make sure to replace command1, command2, command3 with actual commands that you want to execute and assign the output to variables.

How to assign command output to a variable in bash script?

To assign the output of a command to a variable in a bash script, you can use command substitution using backticks or the $() syntax.

Here is an example using backticks:

# Assign the output of the 'date' command to a variable current_date=`date`

echo "Current date is: $current_date"

Here is an example using the $() syntax:

# Assign the output of the 'hostname' command to a variable host_name=$(hostname)

echo "Hostname is: $host_name"

Both of these examples will store the output of the specified command in the variable (current_date and host_name in the above examples) and then you can use the variable in your script as needed.