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 >
- At first, launch an Ubuntu Terminal.
- 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.
- 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.
- Press CTRL+O and ENTER to save the file. Then CTRL+X to exit.
- Run the script by executing the following command:
bash var.sh
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.EXPLANATION- bash: Executes the bash scripts.
- var.sh: Bash script name.
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
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 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
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 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
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 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
Related Articles
- How to Use String Variables in Bash Script? [4 Cases]
- How to Append String to Bash Variable? [2 Effective Ways]
- How to Check If Bash Variable Exists? [2 Effective Methods]
- How To Check if Bash Variable is Empty? [2 Easy Methods]
- How to List and Set Bash Environment Variables? [3 Methods]
- 2 Ways to Unset Environment Variables Using Bash Script
- 5 Methods to Check If Environment Variable is Set in Bash Script
- How to Set Bash $PATH Variable? [Easiest Configuration]
- 2 Cases to Execute Command Stored in Bash Variable
- How to Store Command Output to Bash Variable? [3 Examples]
- How to Read a File into Bash Variable? [2 Simple Methods]
- How to Write Bash Variable to File? [3 Effective Methods]
- Compare Variables in Bash Scripts [3 Practical Cases]
- Increment Variable Value in Bash Scripts [4+ Examples]
- Adding 1 to Bash Variable [3 Examples]
- Decrement Variable Value in Bash Scripts [4+ Examples]
- Addition of Bash Variable [4+ Examples]
- How to Subtract Two Bash Variables? [4+ Easy Approaches]
- How to Multiply Variable in Bash [6+ Practical Examples]
- Variable Substitution in Bash [Replace Character & Substring]
<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners