How to Echo Variables in Bash Script? [4 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Echoing variables in Bash is an essential skill for Bash scriptwriters. In this article, I will explore best practices to effortlessly display variable values. Whether you are a beginner or an experienced scripter, this article will equip you with the knowledge to streamline your Bash scripting. Let’s dive in and master the art of bash echo variable.

Key Takeaways

  • Getting familiar with the Bash variables.
  • Getting familiar with the process of printing Bash variables.

Free Downloads

4 Practical Examples to Echo Variables Using Bash Scripts

In this section, I have listed some practical examples of the echo bash variables. After going through these examples, I believe you will be efficient enough to echo variables using bash scripts.

Example 1: Declaring and Displaying String Data Using Variable in Bash Script

You can set variables in the bash script according to the need and then display the data on the terminal. Here I will develop a bash script in which, firstly set, one variable will be created and then displayed on the terminal.

Steps to Follow >

  1. At first, launch an Ubuntu Terminal.
  2. Write the following command to open a file in Nano:
    nano var.sh
    EXPLANATION
    • nano: Opens the nano text editor.
    • var.sh: Bash script name.
  3. Copy the script mentioned below:
    #!/bin/bash
    
    var="Ubuntu" #setting var variable
    
    echo "$var" #displaying var variable on terminal
    EXPLANATION

    #! /bin/bash ‘#!’, is called shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s bash. Then var=”Ubuntu” set the value of the var variable. Then echo “$var” command displays the value of the var variable on the terminal.

  4. Press CTRL+O and ENTER to save the file. Then CTRL+X to exit.
  5. Run the script by executing the following command:
    bash var.sh
    EXPLANATION
    • bash: Executes the bash scripts.
    • var.sh: Bash script name.
    The bash script has displayed the value of a variable on the terminal.The above image shows that I have set a variable in a bash script and then displayed the value of that variable on the terminal.

Example 2: Print Two Combined String Variables Using a Bash Script

In this example, I will develop a bash script that will combine two string variables. To do so, follow the below script.

Script (comb.sh) >

#!/bin/bash

var1="Price of This Book: $" #setting var1 variable

var2="50" #setting var2 variable

echo "$var1 $var2" #combining the two variable then displaying on the terminal
EXPLANATION

The var1=”Price of This Book: $” command set the value of the var1 variable. Then the var2=”50″ command sets the value of the var2 variable. Then the echo “$var1 $var2” command combines the value of the var1 and var2 variables and then displays it on the terminal.

Run the script by using the following command:

bash comb.sh

The Bash script has combined two variable and printed it on the terminal.The above image shows that the bash script has combined the two variables and printed their values on the terminal.

Example 3: Print Bash Variable Concatenated With Strings

Sometimes programmers may need to concatenate strings with variables. In this example, I will develop a script that will concatenate strings with variables. To do so, follow the below script.

Script (concat.sh) >

#!/bin/bash

var="Good" #setting var variable

echo "$var Morning" #concatenating string with variable then printing
EXPLANATION

The var=”Good” command sets a value to the var variable. Then the echo “$var Morning” command concatenates the string with the var variable and prints on the terminal.

Run the script by executing the following command:

bash concat.sh

The bash script has concatenate two strings and printed it on the terminal.The above image illustrates that the bash script has concatenated the variable with a string and then printed it on the terminal.

Example 4: Echo Numeric Bash Variable After Arithmetic Operation

The programmers may often need to work with numeric data on Bash script. To ease such situations, I have developed a bash script that works with numeric data of variables.

Script (numeric.sh) >

#!/bin/bash

n=130 #setting the value of n 130

((n=n+20)) #add 20 with n and keep the value to n

echo $n #print the value of n on the terminal
EXPLANATION

The n=130 command sets 130 as the value of n. Then the ((n=n+20)) command adds 20 with n and keeps the value to n. Finally, the echo $n command prints the value of n on the terminal

Run the script by using the following command:

bash numeric.sh

The bash script has handled a variable and done some arithmetic operation then print the final value on the terminal.The above image shows that the bash script has handled a numeric variable and done an arithmetic operation, then printed the variable value on the terminal.

Conclusion

In conclusion, by understanding the principles of variable expansion and employing the appropriate syntax, you can effectively display the values of variables. Whether you are a beginner or an experienced user, the techniques discussed in this article give you the knowledge and tools to easily manipulate and display Bash variables. Practice and experiment with different scenarios to solidify your understanding and expand your capabilities. With this newfound skill, you can enhance your scripting abilities and streamline your workflow, ultimately becoming a more efficient and proficient Bash programmer.

People Also Ask

How do you use variables in bash?
Bash script can print out a variable whenever a user requests it. To put it another way, you must use a $ sign before its name. Bash will be informed that the user wants to use a variable value in this manner. The $ symbol is not required to establish a variable or change its value; just the variable’s actual name is required.
What is $$ in the bash script?
Any command or bash script’s process id is referenced by the string $$. In a bash script, $0 is used to retrieve the command’s name. The value of the script’s variable “name” will be printed using the syntax $name.
How to read a variable in bash?
You can use the built-in Bash command read to take user input. It assigns user input to the variable after receiving it. It simply takes one line from the Bash shell to read.
How do I add a variable to a file in bash?
By combining the echo command with the redirect operator, you may write variable data to a file. The -e parameter, in this case, just affects the call to echo; it is not sent to a file.

Related Articles


<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial

Rate this post
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment