30 C
Jaipur
Tuesday, October 20, 2020

Bash how to echo a variable – Linux Hint

Must read

2021 Toyota Innova Scored 5 Star Safety Rating In ASEAN NCAP Crash Test

The 2021 Toyota Innova Crysta facelift recently made its debut in Indonesia. Along with that, the MUV was crash tested by ASEAN NCAP and...

Google Pixel 5 Owners Report Gap Between Display And Frame

Google recently launched the Pixel 5 at $699. Unlike the previous-generation Pixel 4, this year’s flagship from Google doesn’t come with overrated features and...

Some Pixel 5 Owners Report Gap Between the Screen and Body

Google’s product launches always seem to be followed by some hardware issues. Last year’s flagship Pixel 4 users saw the glass back coming loose...

While a user is working bash script execution in a Linux system, different sets of bash commands need to be executed from the terminal window based on the various requirements. After running bash commands, it shows the output on the terminal if no error exists otherwise an error message shows on the command-line window. Sometimes, users want to keep this output for future use. In this situation, the output of these commands can be stored in a variable.

Variables are an essential feature of bash programming in which we assign a label or name to refer to other quantities: such as an arithmetic command or a value. They are used to make the machine programs more readable for humans. Using the echo command you can display the output of a variable or line of text. It requires no formatting while implementing this option. The echo command is useful to display the variable’s output especially when you know the content of a variable will not cause any issue.

In this article, we will explore how to echo a variable in bash. We have implemented all bash commands on Ubuntu 20.04. We will discuss some examples through which you can easily understand the basic concepts.

Basic Syntax

Here, is the basic syntax of how to echo a variable is given below:

In the above command echo is a command that is used for displaying the value of variable ‘var_name’. Var_name is the name of a variable.

Launch terminal

Open the terminal by pressing ‘Ctrl + Alt + t’ or launch terminal from the application search bar. To do that, click on the ‘Activities’ located at the left corner in Ubuntu 20.04 and the write ‘terminal’ in the search bar as follows:

Launch the terminal by clicking on the terminal icon.

Echo Single Variable

Using the echo command you can echo the value of a variable. Just you need to declare and assign value to a variable and then simply echo the value of the variable. For your good understanding, we will discuss some examples which are given below:

Example # 01:

Let’s take an example, we want to display the value of a variable named ‘var_a’ that has a value 100. Now, using the echo command we can simply display its value on the terminal as follows:

The following output you will on the terminal:

Example # 02:

Let’s discuss another example, we want to display the text ‘bash programming echo variable’ on the terminal by using the variable. So, take a variable named ‘var_b’ and store the above text in this variable with double-quotes.

$ var_b=” bash programming echo variable”
$ echo $var_b

You will see the following output on the terminal:

Note: if you will use echo var_b then it will only display the variable name on the terminal instead of displaying its value.

Echo Multiple Variables

The following example will show you how to echo multiples variables:

Example # 01:

For example, take two variables var_A and var_B.

$ var_A=”hellofriends”
$ var_B=50
$ echo $var_A$var_B

The following output will display on the terminal:

Example # 02:

For example, we want to display the date and hostname of our computer. So, we will store the date and hostname commands in var1 and var2 respectively. You can see the implementation as follows:

$ var1=$(date)
$ var2=$(hostname)
$ echo “the date is $var1 @ computer name is $var2

After running the above command, you will see the following output:

Conclusion

In this article, we have shown how to display a variable value or text output by using the echo command. We have executed different bash variables examples on the terminal for better understanding. From the above commands, I hope now you are familiar with how to echo variables and text in bash programming. Furthermore, you can use different commands to store inside the variable. Please, let me know in case of any problem related to this article.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

2021 Toyota Innova Scored 5 Star Safety Rating In ASEAN NCAP Crash Test

The 2021 Toyota Innova Crysta facelift recently made its debut in Indonesia. Along with that, the MUV was crash tested by ASEAN NCAP and...

Google Pixel 5 Owners Report Gap Between Display And Frame

Google recently launched the Pixel 5 at $699. Unlike the previous-generation Pixel 4, this year’s flagship from Google doesn’t come with overrated features and...

Some Pixel 5 Owners Report Gap Between the Screen and Body

Google’s product launches always seem to be followed by some hardware issues. Last year’s flagship Pixel 4 users saw the glass back coming loose...

MX Player Beta Adds Support for YouTube Videos, Pinch to Zoom, and More

A few years back, MX Player transitioned from being just a media player app to a full-fledged streaming platform. In its latest beta build,...