16 C
Jaipur
Friday, November 27, 2020

Bash How to Print a Variable? – Linux Hint

Must read

Alibaba, Tencent put talks to buy iQIYI stake on hold due to price, regulatory concerns: Sources – Latest News

=Alibaba Group Holding Ltd and Tencent Holdings Ltd have each held separate talks with Baidu Inc to acquire a controlling stake in video streaming...

ZTE Watch Live smartwatch with up to 21 days battery life launched in China

ZTE has launched a new smartwatch in its home country China. Called the ZTE Watch Live, the key highlight of...

Realme smartphone 5,000mAh battery spotted on FCC website

Smartphone brand Realme is soon set to launch a new smartphone — bearing the model name RMX3063 — and the same has...

How to Install OpenNMS Network Monitoring Tool in CentOS 8

OpenNMS is a free and open-source network monitoring and network management platform used for managing enterprise networks around the world. It is based on...

Bash scripts are an effective means of increasing efficiency in programming. They also increase reusability to the fullest since when a Bash script is once written, can be executed for as many times as the user wants. In this article, our goal is to learn the method of printing a variable using Bash.

Method of Printing a Variable using Bash:

Note: We will be demonstrating this method using Ubuntu 20.04. However, you can use any other distribution of Linux as well.

In this method, we will explain to you how you can write a Bash script for printing a variable. For proceeding with this method, you will need to follow the steps mentioned below:

Step # 1: Creating a Bash File:

  • Click on the File Manager icon located on your Ubuntu 20.04 taskbar as highlighted in the following image:
  • Once you are there in the Home folder, you will need to create a Bash script file. For doing that, right-click anywhere on space in your Home folder. Select the New Document option from the cascading menu that appears and then choose the Empty Document from the sub-cascading menu. When a new document has been created in your Home folder, rename it as Print.sh. You can also have any other name of your choice. Moreover, we have chosen the Home folder for creating this file just to save ourselves from the inconvenience of giving the path of this file while executing this script via terminal since the Home folder is generally the default path of the Linux operating system. However, you can create your Bash script file anywhere you want. This newly created Bash file is shown in the image below:

Step # 2: Writing a Print Program in a Bash Script:

  • Now double click on this file to open it and type “#!/bin/bash” at the top of this file to indicate that it is a Bash file as shown in the following image:

  • Type the program shown in the image below in your newly created Bash file. In this program, we are taking a number as input from the user and saving it in the variable num. Then we have used the echo command to print the value of this variable. Moreover, we have also used the printf command to serve the very same purpose. After typing in this program in your Bash file, you need to save it by pressing Ctrl +S and then close it.

In this program, the echo command and the printf command is used to print the output on the console. The read command is used to take input from the user whereas the gathered input is stored in a variable followed by this command. In this case, that variable is num. For the sake of your understanding, we would also like to explicitly state that whenever you want to access or display the value stored in a variable, you always have to mention the “$” symbol before that variable as it is also used in this particular program.

Step # 3: Executing the Print Bash Script via the Terminal in Ubuntu 20.04:

  • Now launch the terminal in Ubuntu 20.04 as shown in the following image:

  • Type the following command in your terminal and run it to execute your newly created Bash file:

This command can also be seen in the image below:

  • After executing this command, you will be asked to enter a number of your choice as shown in the image below:

  • Type any number that you like as shown in the following image and then press the Enter key:

  • As soon as you will provide your input, you will be able to see the same value twice on your terminal i.e. once because of the echo command and once because of the printf command. This output is visible in the image shown below:

Conclusion:

Both the echo and the printf commands can be used to print a variable while using Bash. It entirely depends on your personal preference which of these commands you choose to use.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

Alibaba, Tencent put talks to buy iQIYI stake on hold due to price, regulatory concerns: Sources – Latest News

=Alibaba Group Holding Ltd and Tencent Holdings Ltd have each held separate talks with Baidu Inc to acquire a controlling stake in video streaming...

ZTE Watch Live smartwatch with up to 21 days battery life launched in China

ZTE has launched a new smartwatch in its home country China. Called the ZTE Watch Live, the key highlight of...

Realme smartphone 5,000mAh battery spotted on FCC website

Smartphone brand Realme is soon set to launch a new smartphone — bearing the model name RMX3063 — and the same has...

How to Install OpenNMS Network Monitoring Tool in CentOS 8

OpenNMS is a free and open-source network monitoring and network management platform used for managing enterprise networks around the world. It is based on...

Ecommerce: India’s e-commerce festive season sees $8.3 billion worth gross sales: Report – Latest News

India's e-commerce festive sale season from October 15-November 15 this year delivered USD 8.3 billion (about Rs 58,000 crore) worth of gross sales for...