Technique of Printing a Variable utilizing Bash:
Word: We will likely be demonstrating this technique utilizing Ubuntu 20.04. Nevertheless, you need to use some other distribution of Linux as nicely.
On this technique, we are going to clarify to you how one can write a Bash script for printing a variable. For continuing with this technique, you will have to observe the steps talked about beneath:
Step # 1: Making a Bash File:
- Click on on the File Supervisor icon positioned in your Ubuntu 20.04 taskbar as highlighted within the following picture:
- As soon as you might be there within the Residence folder, you will have to create a Bash script file. For doing that, right-click wherever on house in your Residence folder. Choose the New Doc choice from the cascading menu that seems after which select the Empty Doc from the sub-cascading menu. When a brand new doc has been created in your Residence folder, rename it as Print.sh. It’s also possible to have some other title of your alternative. Furthermore, we have now chosen the Residence folder for creating this file simply to save lots of ourselves from the inconvenience of giving the trail of this file whereas executing this script by way of terminal because the Residence folder is usually the default path of the Linux working system. Nevertheless, you’ll be able to create your Bash script file wherever you need. This newly created Bash file is proven within the picture beneath:
Step # 2: Writing a Print Program in a Bash Script:
- Now double click on on this file to open it and kind “#!/bin/bash” on the prime of this file to point that it’s a Bash file as proven within the following picture:
- Kind this system proven within the picture beneath in your newly created Bash file. On this program, we’re taking a quantity as enter from the person and saving it within the variable num. Then we have now used the echo command to print the worth of this variable. Furthermore, we have now additionally used the printf command to serve the exact same function. After typing on this program in your Bash file, you should put it aside by urgent Ctrl +S after which shut it.
On this program, the echo command and the printf command is used to print the output on the console. The learn command is used to take enter from the person whereas the gathered enter is saved in a variable adopted by this command. On this case, that variable is num. For the sake of your understanding, we might additionally wish to explicitly state that everytime you wish to entry or show the worth saved in a variable, you all the time have to say the “$” image earlier than that variable as it’s also used on this specific program.
Step # 3: Executing the Print Bash Script by way of the Terminal in Ubuntu 20.04:
- Now launch the terminal in Ubuntu 20.04 as proven within the following picture:
- Kind the next command in your terminal and run it to execute your newly created Bash file:
This command can be seen within the picture beneath:
- After executing this command, you’ll be requested to enter various your alternative as proven within the picture beneath:
- Kind any quantity that you simply like as proven within the following picture after which press the Enter key:
- As quickly as you’ll present your enter, it is possible for you to to see the identical worth twice in your terminal i.e. as soon as due to the echo command and as soon as due to the printf command. This output is seen within the picture proven beneath:
Each the echo and the printf instructions can be utilized to print a variable whereas utilizing Bash. It totally relies on your private choice which of those instructions you select to make use of.
bash redirect output to variable,bash echo variable in string,bash use variable in command,variable = output of command,store output in string bash,bash execute command in variable,read user input in shell script,bash input arguments,bash script automate user input,bash user input yes/no,read: -p: no coprocess,bash read password,egrep m a states txt,the unix workbench coursera solutions,bash arithmetic,bash script example,assigning command output to variable bash,capturing ssh output as variable in bash script,bash set variable to a command,stdout to variable bash,assign echo output to variable bash,bash assign variable output command,linux execute command and store in variable,how to put linux command output in variable