Whereas a person is working bash script execution in a Linux system, completely different units of bash instructions should be executed from the terminal window based mostly on the assorted necessities. After working bash instructions, it exhibits the output on the terminal if no error exists in any other case an error message exhibits on the command-line window. Generally, customers need to hold this output for future use. On this state of affairs, the output of those instructions will be saved in a variable.Variables are a necessary characteristic of bash programming wherein we assign a label or title to seek advice from different portions: akin to an arithmetic command or a worth. They’re used to make the machine applications extra readable for people. Utilizing the echo command you possibly can show the output of a variable or line of textual content. It requires no formatting whereas implementing this selection. The echo command is helpful to show the variable’s output particularly when you understand the content material of a variable won’t trigger any subject.

On this article, we are going to discover methods to echo a variable in bash. We’ve applied all bash instructions on Ubuntu 20.04. We’ll focus on some examples via which you’ll be able to simply perceive the fundamental ideas.

Primary Syntax

Right here, is the fundamental syntax of methods to echo a variable is given under:

Within the above command echo is a command that’s used for displaying the worth of variable ‘var_name’. Var_name is the title of a variable.

Launch terminal

Open the terminal by urgent ‘Ctrl + Alt + t’ or launch terminal from the applying search bar. To try this, click on on the ‘Actions’ situated on the left nook in Ubuntu 20.04 and the write ‘terminal’ within the search bar as follows:

Linux Hint Bash How to echo a variable

Launch the terminal by clicking on the terminal icon.

Echo Single Variable

Utilizing the echo command you possibly can echo the worth of a variable. Simply you could declare and assign worth to a variable after which merely echo the worth of the variable. On your good understanding, we are going to focus on some examples that are given under:

Instance # 01:

Let’s take an instance, we need to show the worth of a variable named ‘var_a’ that has a worth 100. Now, utilizing the echo command we will merely show its worth on the terminal as follows:

The next output you’ll on the terminal:

Linux Hint Bash How to echo a variable

Instance # 02:

Let’s focus on one other instance, we need to show the textual content ‘bash programming echo variable’ on the terminal through the use of the variable. So, take a variable named ‘var_b’ and retailer the above textual content on this variable with double-quotes.

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

You will note the next output on the terminal:

Linux Hint Bash How to echo a variable

Notice: if you’ll use echo var_b then it can solely show the variable title on the terminal as an alternative of displaying its worth.

Linux Hint Bash How to echo a variable

Echo A number of Variables

The next instance will present you methods to echo multiples variables:

Instance # 01:

For instance, take two variables var_A and var_B.

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

The next output will show on the terminal:

Linux Hint Bash How to echo a variable

Instance # 02:

For instance, we need to show the date and hostname of our pc. So, we are going to retailer the date and hostname instructions in var1 and var2 respectively. You may see the implementation as follows:

$ var1=$(date)
$ var2=$(hostname)
$ echo “the date is $var1 @ pc title is $var2”

After working the above command, you will notice the next output:

Linux Hint Bash How to echo a variable

Conclusion

On this article, we’ve proven methods to show a variable worth or textual content output through the use of the echo command. We’ve executed completely different bash variables examples on the terminal for higher understanding. From the above instructions, I hope now you’re conversant in methods to echo variables and textual content in bash programming. Moreover, you should use completely different instructions to retailer contained in the variable. Please, let me know in case of any drawback associated to this text.

bash echo asterisk,yes please command in linux,bash echo "-ne",bash echo special characters,bash echo command output,bash echo to file,bash internal variables,declare script,bash declare,bash,bash pid of last command,bash variables $0,assign echo output to variable bash,how to echo double quotes in bash,echo command in linux,shell script echo variable,linux echo -e,display message in linux,echo user will result in what output in linux,echo command output bash