AMP AMP

How to write Bash Script with variables on Ubuntu 21.04

To write Bash Script with variables on Ubuntu 21.04

Introduction

Bash scripts are text files containing various commands. We can put any command that can be executed on the terminal into a Bash script. Variables may be added anywhere in a script (or on the command line for that matter) and Bash will replace them with the value of the variable when they are run.

Installation Procedure

Check the OS version

linuxhelp@linuxhelp:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 21.04
Release:	21.04
Codename:	hirsute

Log on to Root user by using the following command

linuxhelp@linuxhelp:~$ sudo -s
[sudo] password for linuxhelp: 

Changing to Home Directory

root@linuxhelp:/home/linuxhelp# cd ~

Creating a script file by using thefollowing command

root@linuxhelp:~# vi test.sh
#!/bin/bash
 
echo Enter your Name
 
read NAME
 
echo Enter your Age
 
read AGE
 
echo Enter your Number
 
read NUM
 
echo Enter your Mail
 
read MAIL
 
echo "Details you provided"
 
echo Your name is $NAME
 
echo Your age is $AGE
 
echo Your Number is $NUM
 
echo Your mail is $MAIL
 
echo Registration completed

Executing Script file by using the following command

root@linuxhelp:~# sh test.sh
Enter your Name
Musk
Enter your Age
25
Enter your Number
987654321
Enter your Mail
musk@gmail.com
Details you provided
Your name is Musk
Your age is 25
Your Number is 987654321
Your mail is musk@gmail.com
Registration completed

By this writing Bash Script with variables comes to an End

FAQ
Q
What is shell script extension?
A
The shell script extension is .sh
Q
What is variable shell scripting?
A
A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data
Q
What is the difference between shell and terminal?
A
A shell is a user interface for access to an operating system's services. ... The terminal is a program that opens a graphical window and lets you interact with the shell.
Q
Is Bourne Again a Linux shell?
A
Bash (Bourne Again Shell ) is the free version of the Bourne shell distributed with Linux and GNU operating
Q
What is shell scripting?
A
A shell script is a text file that contains a sequence of commands for a UNIX-based operating system.