Followers

Linux - Shell Script Variables

  A variable is a character string to which we assign a value. The value assigned could be number, text, filename etc.  A variable is nothin...


 A variable is a character string to which we assign a value. The value assigned could be number, text, filename etc. 

A variable is nothing more than a pointer to the actual data.

Variable Names:

The name of a variable can contain only letters (a-z,A-z), numbers (0 to 9) or underscore character(_)

For example below are the valid variable names

  • name
  • Token_A
  • VAR1

Below are invalid variables

  • 1Var
  • Var-var1

Define and Access the variable

You can define the variable using

varname = variablevalue

for example

name="Raman"

To Read the variable at runtime 

read name

To Access the name variable you need to use $ symbol before variable name

echo $name

Read-only Variables

If a variable is read-only then you cannot change the value of the variable

#! /bin/bash

#Author Raman
# Copyright (c)  Raman
# Below is the scripti
name="raman"
readonly name
name="test"
# This will print the  name of the person
echo $name


It will not allow to change the value of name variable.

Unsetting the variable

unset command is used to unset the variable value but you cannot unset a readonly variable.

name="raman"
name="test"
unset name
# This will print the  name of the person
echo $name


Special Type of Variable in Shell Script

$0:- Name of the file

$n:- It corresponds to the respective command line argument

$#:- The number of supplied arguments to the script

$*:- All the arguments are double-quoted.

$@:- All the arguments are individually double-quoted.

$?:- The exit status of the last command executed.

$$:- The process number of the current shell

$!: The process number of the last background command.

Example

echo "File name = $0"
echo "First argument = $1"
echo "Second argument = $2"
echo "Number of arguments = $#"
echo "All the arguments = $*"
echo "Each argument = $@"
echo "Exit status of last command = $?"
echo "Process number of current shell = $$"
echo "Process number of the last background command = $!"

Execute the script

./test.sh "First Argurment" "Second Argument"

Output

File name = ./test.sh

First argument = First Argurment

Second argument = Second Argument

Number of arguments = 2

All the arguments = First Argurment Second Argument

Each argument = First Argurment Second Argument

Exit status of last command = 0

Process number of current shell = 3091

Process number of the last background command =

Example

To Print the grade of a student on the basis of command line argument

if [ $2 -gt 80 ]
then
        echo "$1 Got A Grade"
elif [ $2 -gt 70 ]
then
        echo "$1 Got B Grade"
else
        echo "$1 Got C Grade"
fi

Execute 

sh test.sh raman 90

Output

raman Got A Grade

Example

Comparing two strings

VAR1="$1"
VAR2="$2"

if [ "$VAR1" = "$VAR2" ]
then
    echo "Strings are equal."
else
    echo "Strings are not equal."
fi

Execute

sh test.sh First First

Strings are equal

Assignment

  • Write a program to find how many command-line argument are passed while executing the script if the number of passed arguments is more than 3 then print a message "Too many arguments are passed" otherwise print " You are good to go"

if [ $# -gt 3 ]
then
        echo "Too many arguments"
else
        echo "Good to go"
fi

  • Pass 2 numeric arguments to a program script on execution and find the greater number.
if [ $1 -gt $2 ]
then
        echo "$1 is greater than $2"
else
        echo "$2 is greater than $1"
fi


expr

It evaluates the given expression and displays its corresponding output. It is mainly used for addition, subtraction, multiplication, division etc. It also does the regular expression for string operations like substring, length of the string etc

expr --version

expr --help

expr 12 + 10

expr 12 \* 3

Example

To find the sum of 2 numbers

echo "Enter 2 numbers"
read x
read y
sum=`expr $x + $y`
echo "Sum= $sum"

Example 

Find the total number of lines of 2 files and print sum of these total lines. 

var1=`cat test.sh | wc -l`
var2=`cat sum.sh | wc -l`
echo "Total lines in test.sh = $var1"
echo "Total lines in sum.sh = $var2"

var3=`expr $var1 + $var2`
echo "Total lines are $var3"

Example

Find the total number of lines of 2 files that are passed as command-line arguments and print the sum of these total lines. 

var1=`cat $1  | wc -l`
var2=`cat $2 | wc -l`
echo "Total lines in $1 = $var1"
echo "Total lines in $2 = $var2"

var3=`expr $var1 + $var2`
echo "Total lines are $var3"


Example

Write a program to find "unix" word in a file and if it is found then print it as a unix file otherwise print it is not a unix file ( using hardcode values for the file name and search char/ using command-line argument)

var1=`grep unix myfile.txt | wc -l`

echo "matching word count= $var1"
if [ $var1 -gt 0 ]
then
        echo " word is found"
else
        echo "word is not found"
fi

Example

Search the total number of characters in a file and also pass the code coverage percentage value if the total number of characters is less than code coverage then printing that code coverage is not possible.

var1=`cat $1 | wc -c`
echo "Number of characters = $var1"
if [ $var1 -lt $2 ]
then
        echo "Code coverage is not possible"
else
        echo "Please go ahead with code coverage"
fi

Execute

sh count.sh myfile.txt 90

Number of characters = 47

Please go ahead with code coverage


----



 





COMMENTS

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,git quiz,1,Git Worksheet,1,ITIL,1,ITSM,1,Jira,3,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SDLC Quiz,5,SonarQube,3,Splunk,2,vagrant kubernetes,1,Windows,1,YAML Basics,1,
ltr
item
DevOpsWorld: Linux - Shell Script Variables
Linux - Shell Script Variables
DevOpsWorld
https://www.devopsworld.co.in/2022/04/linux-shell-script-variables.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/2022/04/linux-shell-script-variables.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content