2
Most read
4
Most read
5
Most read
Linux Shell script (Bash) Basics
PART - 1
1
2
 A script is a list of system commands stored in a
file.
 Steps to write a script :-
 Use any editor like vi or vim
 chmod permission your-script-name.
 Examples:-

$ chmod +x <filename.sh>

$ chmod 755 <filename.sh>
 Execute your script as:
 $ bash filename.sh
 $ bash fileneme.sh
 $ ./filename.sh
3
 My first shell script
clear
 echo “hello world“
 $ ./first
 $ chmod 755 first
 $ ./first
 Variables in Shell:
 In Linux (Shell), there are two types of variable:
 (1) System variables :
 (2) User defined variables :
4
 $ echo $USERNAME
 $ echo $HOME
 User defined variables :
 variable name=value
 Examples:
 $x = 10
 echo Command:
 echo command to display text
 arithmetic operations
 Syntax:
expr op1 math-operator op2
Examples:
$ expr 10 + 30
$ expr 20 – 10
$ expr 100 / 20
$ expr 200 % 30
$ expr 100 * 30
$ echo `expr 60 + 30`
5
Renaming files
mv test1 test2
Deleting files
rm -i test1
Creating directories
mkdir dir3
Deleting directories
rmdir dir3
6
 $ ps
PID TTY TIME CMD
 $ ps -ef
UID PID PPID C STIME TTY TIME CMD
 $ ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME
CMD
 $ ps -efH
UID PID PPID C STIME TTY TIME CMD
7
Creating files:
$ touch test1
$ ls -il test1
Copying files:
cp source destination
cp test1 test2
Linking files:
There are two different types of file links in Linux:
a. A symbolic, or soft, link
b. A hard link
8
" Double
Quotes
Double Quotes" -
Anything enclose in
double quotes removed
meaning of that
characters (except  and
$).
' Single quotes 'Single quotes' - Enclosed in
single quotes remains
unchanged.
` Back quote `Back quote` - To execute
command
9
 Pipes:
who | wc –l
 Reading from Files:
$ read message
$ echo $message
Read command to read lines from files
 Command substitution:
Var=`date`
Var=$(date)
 Background Processes:
 ls -R /tmp &
1
0
while read ip name alias
do
if [ ! -z “$name” ]; then
# Use echo -en here to suppress ending the line;
# aliases may still be added
echo -en “IP is $ip - its name is $name”
if [ ! -z “$aliases” ]; then
echo “ Aliases: $aliases”
else
# Just echo a blank line
echo
fi
fi
done < /etc/hosts
1
1
Stopping processes
kill pid
disk space
$ df
$ df –h
Disk usages:
$ du
Commands:
$ cat file1
$ sort file1
$ cat file2
$ sort file2
$ sort -n file2
1
2
 grep [options] pattern [file]
 The grep command searches either the input
or the file you specify for lines that contain
characters that match the specified pattern.
The output from grep is the lines that contain
the matching pattern.
1
3
$ cat random.sh
#!/bin/bash
MIN=200
MAX=500
let “scope = $MAX - $MIN”
if [ “$scope” -le “0” ]; then
echo “Error - MAX is less than MIN!”
fi
for i in `seq 1 10`
do
let result=”$RANDOM % $scope + $MIN”
echo “A random number between $MIN and $MAX is $result”
Done
$ ./random.sh
1
4
$ cat hypotenuse.sh
#!/bin/sh
# calculate the length of the hypotenuse of a Pythagorean
triangle
# using hypotenuse^2 = adjacent^2 + opposite^2
echo -n “Enter the Adjacent length: “
read adjacent
echo -n “Enter the Opposite length: “
read opposite
osquared=$(($opposite ** 2)) # get o^2
asquared=$(($adjacent ** 2)) # get a^2
hsquared=$(($osquered + $asquared)) # h^2 = a^2 + o^2
hypotenuse=`echo “scale=3;sqrt ($hsquared)” | bc`
# bc does sqrt
echo “The Hypotenuse is $hypotenuse”
1
5
 There are two types of environment variables in the
bash shell
 Global variables
 Local variables
1
6
 An array is a variable that can hold multiple values.
 To set multiple values for an environment variable, just list
them in parentheses, with each value
 separated by a space:
 $ mytest=(one two three four five)
 $
 Not much excitement there. If you try to display the array
as a normal environment variable,
 you’ll be disappointed:
 $ echo $mytest
 one
 $
 Only the first value in the array appears. To reference an
individual array element, you must use
 a numerical index value, which represents its place in the
array. The numeric value is enclosed in
 square brackets:
 $ echo ${mytest[2]}
 three
 $
1
7
$ date ; who
$ chmod u+x test1
$ ./test1
$ echo This is a test
This is a test
$ echo Let’s see if this’ll work
Lets see if thisll work
$
1
8
One of the most useful features of shell scripts is the lowly
back quote character, usually called the
backtick (`) in the Linux world.
You must surround the entire command line command with
backtick characters:
testing=`date`
$ cat test5
#!/bin/bash
# using the backtick character
testing=`date`
echo "The date and time are: " $testing
$
1
9
 Output redirection
The most basic type of redirection is sending output from a
command to a file. The bash shell uses the greater-than
symbol for this:
command > outputfile
Input redirection
Input redirection is the opposite of output redirection
The input redirection symbol is the less-than symbol (<):
command < inputfile
2
0
$ expr 1 + 5
6
The bash shell includes the expr command to stay
compatible with the Bourne shell; however, it
also provides a much easier way of performing mathematical
equations
$ var1=$[1 + 5]
$ echo $var1
6
$ var2 = $[$var1 * 2]
$ echo $var2
12
$
2
1

More Related Content

PPTX
Bash Shell Scripting
PPTX
Shell scripting
PPT
Shell Scripting
PDF
Course 102: Lecture 5: File Handling Internals
PPTX
Easiest way to start with Shell scripting
PDF
Course 102: Lecture 2: Unwrapping Linux
PPT
Basic 50 linus command
PDF
Course 102: Lecture 7: Simple Utilities
Bash Shell Scripting
Shell scripting
Shell Scripting
Course 102: Lecture 5: File Handling Internals
Easiest way to start with Shell scripting
Course 102: Lecture 2: Unwrapping Linux
Basic 50 linus command
Course 102: Lecture 7: Simple Utilities

What's hot (20)

PDF
Shell scripting
PPTX
SHELL PROGRAMMING
PDF
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
PDF
Uboot startup sequence
PDF
Course 102: Lecture 13: Regular Expressions
PPT
Shell Scripting in Linux
PDF
Linux basic commands with examples
ODP
Linux commands
PPTX
Introduction to ansible
PDF
Linux Bash Shell Cheat Sheet for Beginners
PDF
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
PDF
Linux systems - Linux Commands and Shell Scripting
PDF
Course 102: Lecture 3: Basic Concepts And Commands
PDF
Build your own embedded linux distributions by yocto project
PDF
Linux Kernel - Virtual File System
PDF
Unix Command-Line Cheat Sheet BTI2014
PPTX
Basic commands of linux
PDF
DWARF Data Representation
PDF
Basic linux commands
PPT
Introduction to PowerShell
Shell scripting
SHELL PROGRAMMING
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
Uboot startup sequence
Course 102: Lecture 13: Regular Expressions
Shell Scripting in Linux
Linux basic commands with examples
Linux commands
Introduction to ansible
Linux Bash Shell Cheat Sheet for Beginners
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
Linux systems - Linux Commands and Shell Scripting
Course 102: Lecture 3: Basic Concepts And Commands
Build your own embedded linux distributions by yocto project
Linux Kernel - Virtual File System
Unix Command-Line Cheat Sheet BTI2014
Basic commands of linux
DWARF Data Representation
Basic linux commands
Introduction to PowerShell
Ad

Viewers also liked (18)

PPTX
Shell Script Tutorial
PDF
shell script introduction
PDF
The bash – defensive scripting
PDF
Bash Scripting Workshop
PPTX
Bash script
PDF
BASH Guide Summary
PPS
UNIX - Class1 - Basic Shell
PDF
Quick start bash script
PPT
Basic Unix
PDF
Intro to Linux Shell Scripting
PDF
Unix Shell Scripting
PPT
Unix Shell Scripting Basics
PPT
Shell programming
PDF
Linux fundamental - Chap 14 shell script
PPT
Unix/Linux Basic Commands and Shell Script
PDF
Shell Scripting
ODP
Linux Introduction (Commands)
PPTX
UNIX/Linux training
Shell Script Tutorial
shell script introduction
The bash – defensive scripting
Bash Scripting Workshop
Bash script
BASH Guide Summary
UNIX - Class1 - Basic Shell
Quick start bash script
Basic Unix
Intro to Linux Shell Scripting
Unix Shell Scripting
Unix Shell Scripting Basics
Shell programming
Linux fundamental - Chap 14 shell script
Unix/Linux Basic Commands and Shell Script
Shell Scripting
Linux Introduction (Commands)
UNIX/Linux training
Ad

Similar to Bash Shell Scripting (20)

PPT
PPT
390aLecture05_12sp.ppt
PPT
Introduction to shell scripting ____.ppt
PPTX
Linux Shell Scripting
PDF
Shell scripting
PPT
Examples -partII
PPT
Spsl by sasidhar 3 unit
PDF
Basic shell programs assignment 1_solution_manual
PPTX
Shell scripting
PPTX
Basics of shell programming
PPTX
Basics of shell programming
PPTX
First steps in C-Shell
PDF
Basic linux commands
PDF
Shell Scripting crash course.pdf
PPTX
shellScriptAlt.pptx
PPTX
Licão 09 variables and arrays v2
PDF
Unit 11 configuring the bash shell – shell script
PPT
Shell Scripts
PPT
34-shell-programming.ppt
390aLecture05_12sp.ppt
Introduction to shell scripting ____.ppt
Linux Shell Scripting
Shell scripting
Examples -partII
Spsl by sasidhar 3 unit
Basic shell programs assignment 1_solution_manual
Shell scripting
Basics of shell programming
Basics of shell programming
First steps in C-Shell
Basic linux commands
Shell Scripting crash course.pdf
shellScriptAlt.pptx
Licão 09 variables and arrays v2
Unit 11 configuring the bash shell – shell script
Shell Scripts
34-shell-programming.ppt

More from Raghu nath (20)

PPTX
Mongo db
PDF
Ftp (file transfer protocol)
PDF
MS WORD 2013
PDF
Msword
PDF
Ms word
PDF
Javascript part1
PDF
Regular expressions
PDF
Selection sort
PPTX
Binary search
PPTX
JSON(JavaScript Object Notation)
PDF
Stemming algorithms
PPTX
Step by step guide to install dhcp role
PPTX
Network essentials chapter 4
PPTX
Network essentials chapter 3
PPTX
Network essentials chapter 2
PPTX
Network essentials - chapter 1
PPTX
Python chapter 2
PPTX
python chapter 1
PPTX
PPTX
Adv excel® 2013
Mongo db
Ftp (file transfer protocol)
MS WORD 2013
Msword
Ms word
Javascript part1
Regular expressions
Selection sort
Binary search
JSON(JavaScript Object Notation)
Stemming algorithms
Step by step guide to install dhcp role
Network essentials chapter 4
Network essentials chapter 3
Network essentials chapter 2
Network essentials - chapter 1
Python chapter 2
python chapter 1
Adv excel® 2013

Recently uploaded (20)

PDF
Consumable AI The What, Why & How for Small Teams.pdf
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PPTX
The various Industrial Revolutions .pptx
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
sbt 2.0: go big (Scala Days 2025 edition)
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
Flame analysis and combustion estimation using large language and vision assi...
PDF
Comparative analysis of machine learning models for fake news detection in so...
PDF
STKI Israel Market Study 2025 version august
PDF
OpenACC and Open Hackathons Monthly Highlights July 2025
PDF
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
PDF
Convolutional neural network based encoder-decoder for efficient real-time ob...
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PPTX
Training Program for knowledge in solar cell and solar industry
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
Five Habits of High-Impact Board Members
PPTX
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
PPTX
TEXTILE technology diploma scope and career opportunities
PPTX
Build Your First AI Agent with UiPath.pptx
PDF
The influence of sentiment analysis in enhancing early warning system model f...
Consumable AI The What, Why & How for Small Teams.pdf
NewMind AI Weekly Chronicles – August ’25 Week III
The various Industrial Revolutions .pptx
Taming the Chaos: How to Turn Unstructured Data into Decisions
sbt 2.0: go big (Scala Days 2025 edition)
Benefits of Physical activity for teenagers.pptx
Flame analysis and combustion estimation using large language and vision assi...
Comparative analysis of machine learning models for fake news detection in so...
STKI Israel Market Study 2025 version august
OpenACC and Open Hackathons Monthly Highlights July 2025
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
Convolutional neural network based encoder-decoder for efficient real-time ob...
sustainability-14-14877-v2.pddhzftheheeeee
Training Program for knowledge in solar cell and solar industry
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Five Habits of High-Impact Board Members
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
TEXTILE technology diploma scope and career opportunities
Build Your First AI Agent with UiPath.pptx
The influence of sentiment analysis in enhancing early warning system model f...

Bash Shell Scripting

  • 1. Linux Shell script (Bash) Basics PART - 1 1
  • 2. 2  A script is a list of system commands stored in a file.  Steps to write a script :-  Use any editor like vi or vim  chmod permission your-script-name.  Examples:-  $ chmod +x <filename.sh>  $ chmod 755 <filename.sh>  Execute your script as:  $ bash filename.sh  $ bash fileneme.sh  $ ./filename.sh
  • 3. 3  My first shell script clear  echo “hello world“  $ ./first  $ chmod 755 first  $ ./first  Variables in Shell:  In Linux (Shell), there are two types of variable:  (1) System variables :  (2) User defined variables :
  • 4. 4  $ echo $USERNAME  $ echo $HOME  User defined variables :  variable name=value  Examples:  $x = 10  echo Command:  echo command to display text
  • 5.  arithmetic operations  Syntax: expr op1 math-operator op2 Examples: $ expr 10 + 30 $ expr 20 – 10 $ expr 100 / 20 $ expr 200 % 30 $ expr 100 * 30 $ echo `expr 60 + 30` 5
  • 6. Renaming files mv test1 test2 Deleting files rm -i test1 Creating directories mkdir dir3 Deleting directories rmdir dir3 6
  • 7.  $ ps PID TTY TIME CMD  $ ps -ef UID PID PPID C STIME TTY TIME CMD  $ ps -l F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD  $ ps -efH UID PID PPID C STIME TTY TIME CMD 7
  • 8. Creating files: $ touch test1 $ ls -il test1 Copying files: cp source destination cp test1 test2 Linking files: There are two different types of file links in Linux: a. A symbolic, or soft, link b. A hard link 8
  • 9. " Double Quotes Double Quotes" - Anything enclose in double quotes removed meaning of that characters (except and $). ' Single quotes 'Single quotes' - Enclosed in single quotes remains unchanged. ` Back quote `Back quote` - To execute command 9
  • 10.  Pipes: who | wc –l  Reading from Files: $ read message $ echo $message Read command to read lines from files  Command substitution: Var=`date` Var=$(date)  Background Processes:  ls -R /tmp & 1 0
  • 11. while read ip name alias do if [ ! -z “$name” ]; then # Use echo -en here to suppress ending the line; # aliases may still be added echo -en “IP is $ip - its name is $name” if [ ! -z “$aliases” ]; then echo “ Aliases: $aliases” else # Just echo a blank line echo fi fi done < /etc/hosts 1 1
  • 12. Stopping processes kill pid disk space $ df $ df –h Disk usages: $ du Commands: $ cat file1 $ sort file1 $ cat file2 $ sort file2 $ sort -n file2 1 2
  • 13.  grep [options] pattern [file]  The grep command searches either the input or the file you specify for lines that contain characters that match the specified pattern. The output from grep is the lines that contain the matching pattern. 1 3
  • 14. $ cat random.sh #!/bin/bash MIN=200 MAX=500 let “scope = $MAX - $MIN” if [ “$scope” -le “0” ]; then echo “Error - MAX is less than MIN!” fi for i in `seq 1 10` do let result=”$RANDOM % $scope + $MIN” echo “A random number between $MIN and $MAX is $result” Done $ ./random.sh 1 4
  • 15. $ cat hypotenuse.sh #!/bin/sh # calculate the length of the hypotenuse of a Pythagorean triangle # using hypotenuse^2 = adjacent^2 + opposite^2 echo -n “Enter the Adjacent length: “ read adjacent echo -n “Enter the Opposite length: “ read opposite osquared=$(($opposite ** 2)) # get o^2 asquared=$(($adjacent ** 2)) # get a^2 hsquared=$(($osquered + $asquared)) # h^2 = a^2 + o^2 hypotenuse=`echo “scale=3;sqrt ($hsquared)” | bc` # bc does sqrt echo “The Hypotenuse is $hypotenuse” 1 5
  • 16.  There are two types of environment variables in the bash shell  Global variables  Local variables 1 6
  • 17.  An array is a variable that can hold multiple values.  To set multiple values for an environment variable, just list them in parentheses, with each value  separated by a space:  $ mytest=(one two three four five)  $  Not much excitement there. If you try to display the array as a normal environment variable,  you’ll be disappointed:  $ echo $mytest  one  $  Only the first value in the array appears. To reference an individual array element, you must use  a numerical index value, which represents its place in the array. The numeric value is enclosed in  square brackets:  $ echo ${mytest[2]}  three  $ 1 7
  • 18. $ date ; who $ chmod u+x test1 $ ./test1 $ echo This is a test This is a test $ echo Let’s see if this’ll work Lets see if thisll work $ 1 8
  • 19. One of the most useful features of shell scripts is the lowly back quote character, usually called the backtick (`) in the Linux world. You must surround the entire command line command with backtick characters: testing=`date` $ cat test5 #!/bin/bash # using the backtick character testing=`date` echo "The date and time are: " $testing $ 1 9
  • 20.  Output redirection The most basic type of redirection is sending output from a command to a file. The bash shell uses the greater-than symbol for this: command > outputfile Input redirection Input redirection is the opposite of output redirection The input redirection symbol is the less-than symbol (<): command < inputfile 2 0
  • 21. $ expr 1 + 5 6 The bash shell includes the expr command to stay compatible with the Bourne shell; however, it also provides a much easier way of performing mathematical equations $ var1=$[1 + 5] $ echo $var1 6 $ var2 = $[$var1 * 2] $ echo $var2 12 $ 2 1