Mint

How to Use the Echo Command in Bash Scripts in Linux Mint 21?

“Echo is a very interesting command in Linux that prints everything that you wish on the terminal. This command can be used directly within the terminal. However, you can even use it inside Bash scripts. In this article, we will be discussing the usage of this command within the Bash scripts in Linux Mint 21.”

Examples of Using the Echo Command in Bash Scripts in Linux Mint 21

To teach you the usage of the Echo command in Bash scripts in Linux Mint 21, we have designed the following three examples for you:

Example # 1: Printing a Simple Message on the Terminal

In this example, we will create a Bash script that will make use of the Echo command to print a simple message on the terminal. For that, we have created the Bash script shown in the image below:

To execute this Bash script, we ran the following command in the terminal:

$ bash echo.bash

The message printed on the terminal is shown in the image below:

Example # 2: Printing the Result of a Calculation on the Terminal

Now, we will go a step further and create a complex Bash script, as shown in the following image:

In this script, we have created two variables, “a” and “b,” and have assigned them two different values, i.e., 2 and 4. Then, we have created another variable, “sum,” and have equalized it to the expression “$((a+b))” that will store the sum of “a” and “b” to the “sum” variable. Finally, we have used an Echo command to print the sum of “a” and “b” on the terminal.

When we executed this Bash script with the same command that has been shared in the example above, the value of “sum” appeared on the terminal, as shown in the image below:

Example # 3: Making a User Interactive Program

Now, we will try to make an interactive program with the help of the Echo command in Bash that will take some input from the user and then display the results accordingly. The following Bash script will serve this purpose:

This Bash script will prompt the user to enter his name in the terminal. After that, this name will be processed by the “read” command. Then, this Bash script will greet the user with his/ her name by making use of the Echo command.

When we executed this script by using the command shared in our first example, we were prompted to enter the user name as shown in the image below:

The name that we entered is shown in the following image:

Once we entered a user name, a greeting message was printed on the terminal, as shown in the image below:

Conclusion

In this article, we discussed the different usage scenarios of the Echo command within the Bash scripts in Linux Mint 21. Once you go through all these examples, you will realize how easy it is to use this command in Linux.

 

Similar Posts