25 C
Jaipur
Sunday, November 29, 2020

Bash How to Assign Output to a Variable? – Linux Hint

Must read

This Computer Chip Simulates the Future Faster Than Physics

As we are moving towards the future, computer companies are making their chipsets more and more powerful. For instance, Apple’s latest M1 chip has...

20 hot Amazon Black Friday deals you can still get ahead of Cyber Monday 2020

With Cyber Monday approaching in the UK, Amazon's best Black Friday deals are mostly still in stock going into Sunday. While we've seen sellouts...

This VR App Can Take You Places Using Google’s Street View

This year, as we all know, has been one of the most disastrous years in the history of our world. Apart from the ongoing...

This iOS Simulator Game Lets You Fly the SpaceX Starship

If you’ve ever wished to fly one of SpaceX rockets while watching a NASA live stream, then I feel you. However, recently I came...

The Bash acts as the default command-line interpreter in most of the Linux distributions including Ubuntu 20.04. The Bash commands are considered as very powerful and efficient in terms of their processing. At times while running different commands in Bash, you need to save the output of a command to a variable for using it later on for some other purpose. Therefore, in this article, we will explore the method of assigning the output to a variable in Bash.

Method of Assigning Output to a Variable in Bash:

Note: We will be demonstrating this method on Ubuntu 20.04. You can also use any other Linux distribution if you want.

In this method, we will be explaining to you how you can assign the output of a command to a variable in Bash directly via the terminal. For getting this work done through this method, you will have to perform the following steps:

  • Since we are not creating a Bash script for this method, therefore, we should directly launch the terminal in Ubuntu 20.04. You can also take a look at the terminal window from the image shown below:

Method of Storing the Value of “date” Command to a Variable:

  • Once you have launched the terminal, you need to save the output of a command to a variable. For this particular example, we will be using the date command of Linux and we will be assigning its output to the date variable. This can be done by typing the following command in your terminal and then pressing the Enter key:

Here, the first date refers to the variable that we have created. You can have any name of your choice for this variable. Whereas the second date followed by the “$” and enclosed in the round brackets indicate the date command of the Linux operating system. Running this command will simply fetch the current system date and will save it to the date variable. This command can also be seen in the following image:

  • Now type the following command in your terminal and then press the Enter key:

Here, the echo command will be responsible for displaying the current date on your terminal by printing the value of the date variable. You can also use the printf command to achieve the same purpose. This command can also be witnessed from the image shown below:

  • As soon as the execution of the echo command will complete, you will be able to see the current date stored in the date variable on your terminal as shown in the following image. However, the echo command used in this method is not mandatory. It is used just to show you that the output of the date command has been successfully saved to the date variable so that you can easily verify it. Otherwise, you can even skip this step.

Method of Assigning the Value of “who” Command to a Variable:

  • You can also assign the output of the “who” command to a variable. The “who” command is used to find out the currently logged in user of the system. You only need to type the following command in your terminal:

The details of the current user reside in the /usr/bin directory, therefore, this path is mentioned before the “who” command. Running the above-mentioned command will assign the output of the “who” command to the variable named user.

  • After doing this assignment, you have to run the echo command to print this value as shown in the image below:

  • The execution of the echo command will display all the information regarding the currently logged in user along with the current system date and time on your terminal as shown in the following image:

Method of Saving the Value of “pwd” Command to a Variable:

  • You can even find out the current working directory of your Ubuntu 20.04 system and store it in a variable. All you have to do is to run the command shown below:

Running this command will store your current working directory in the working_directory variable.

  • After assigning the current working directory to your desired variable, you can display the value of this variable on the terminal by making use of the echo command:

  • Executing the echo command will display the current working directory on your terminal as shown in the following image:

In the very same manner, you can assign the outputs of even the complex commands to any variable of your choice.

Conclusion:

By following the easy and simple steps described in this article, one can conveniently store the output of any command in Bash to the desired variable. This variable can then be used for printing the output of that command or even using it for any further processing.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

This Computer Chip Simulates the Future Faster Than Physics

As we are moving towards the future, computer companies are making their chipsets more and more powerful. For instance, Apple’s latest M1 chip has...

20 hot Amazon Black Friday deals you can still get ahead of Cyber Monday 2020

With Cyber Monday approaching in the UK, Amazon's best Black Friday deals are mostly still in stock going into Sunday. While we've seen sellouts...

This VR App Can Take You Places Using Google’s Street View

This year, as we all know, has been one of the most disastrous years in the history of our world. Apart from the ongoing...

This iOS Simulator Game Lets You Fly the SpaceX Starship

If you’ve ever wished to fly one of SpaceX rockets while watching a NASA live stream, then I feel you. However, recently I came...

iOS 15: what we want to see

iOS 15 is a long way off yet but we’re already hearing the first early leaks and rumors about it, all of which you’ll...