SlideShare a Scribd company logo
RedHat Enterprise Linux Essential
Unit 11: Configuring the Bash Shell – Shell
                   script
Objectives

Upon completion of this unit, you should be able to:

 Know how to use local and environment variables

 Know how to inhibit variable expansion

 Know how to create aliases

 Understand how the shell parses a command line

 Know how to configure startup files

 Know how to handle input with the read command and
  positional parameters
Bash Variables

 Variables are named values
    Useful for storing data or command output

 Set with VARIABLE=VALUE

 Referenced with $VARIABLE


  $ HI="Hello, and welcome to $(hostname)."
  $ echo $HI
  Hello, and welcome to stationX.
Environment Variables

 Variables are local to a single shell by default

 Environment variables are inherited by child shells
    Set with export VARIABLE=VALUE

    Accessed by some programs for configuration
Some Common Variables

 Configuration variables
    PS1: Appearance of the bash prompt

    PATH: Directories to look for executables in

    EDITOR: Default text editor

    HISTFILESIZE: Number of commands in bash history

 Information variables
    HOME: User's home directory

    EUID: User's effective UID
example PS1

syntax:     PS1='[display content]'
   !                 Display history number
   #                 Display number of current command
   $                 Display $ or #
                    Display symbol 
   d                 Display current date
   h                 Display hostname
   s                 Display shell
   t                 Display current time
   u                 Display user
   W                 Display home work current
   w                 Display full path home work current

PS1='t u@h s $'
Aliases

 Aliases let you create shortcuts to commands

       $ alias dir='ls -laht'

 Use alias by itself to see all set aliases

 Use alias followed by an alias name to see alias value

       $ alias dir

           alias dir='ls -laht'
How bash Expands a Command Line
   Split the line into words
   Expand aliases
   Expand curly-brace statements ({})
   Expand tilde statements (~)
   Expand variables ($)
   Command-substituation ($() and ``)
   Split the line into words again
   Expand file globs (*, ?, [abc], etc)
   Prepare I/O redirections (<, >)
   Run the command!
Preventing Expansion

 Backslash (  ) makes the next character literal
       $ echo Your cost: $5.00
        Your cost: $5.00
 Quoting prevents expansion
    Single quotes (') inhibit all expansion

    Double quotes (") inhibit all expansion, except:
       • $ (dollar sign) - variable expansion

       • ` (backquotes) - command substitution

       •  (backslash) - single character inhibition

       • ! (exclamation point) - history substitution
Login vs non-login shells

 Startup is configured differently for login and non-login shells
 Login shells are:
    Any shell created at login (includes X login)

    su –
 Non-login shells are:
    su

    graphical terminals

    executed scripts

    any other bash instances
Bash startup tasks: profile


 Stored in /etc/profile (global) and ~/.bash_profile (user)

 Run for login shells only

 Used for
    Setting environment variables

    Running commands (eg mail-checker script)
Bash startup tasks: bashrc


 Stored in /etc/bashrc (global) and ~/.bashrc (user)

 Run for all shells

 Used for
    Setting local variables

    Defining aliases
Bash exit tasks


 Stored in ~/.bash_logout (user)

 Run when a login shell exits

 Used for
    Creating automatic backups

    Cleaning out temporary files
Scripting: Taking input
                with positional Parameters
 Positional parameters are special variables that hold the
  command-line arguments to the script.

 The positional parameters available are $1, $2, $3, etc. .
  These are normally assigned to more meaningful variable
  names to improve clarity.

 $* holds all command-line arguments

 $# holds the number of command-line arguments
Scripting: Taking input with the read command

 Use read to assign input values to one or more shell variables:
    -p designates prompt to display

    read reads from standard input and assigns one word to each variable

    Any leftover words are assigned to the last variable

    read -p "Enter a filename: " FILE
   #!/bin/bash
   read -p "Enter several values:" value1 value2
   value3
   echo "value1 is $value1"
   echo "value2 is $value2"
   echo "value3 is $value3"
While
 #!/bin/bash
  # SCRIPT: method1.sh
  # PURPOSE: Process a file line by line with PIPED while-
  read loop.

  FILENAME=$1
  count=0
  cat $FILENAME | while read LINE
  do
  let count++
  echo "$count $LINE"
  done

  echo -e "nTotal $count Lines read"
While with redirect
 #!/bin/bash
  #SCRIPT: method2.sh
  #PURPOSE: Process a file line by line with redirected
  while-read loop.

  FILENAME=$1
  count=0

  while read LINE
  do
  let count++
  echo "$count $LINE"

  done < $FILENAME

  echo -e "nTotal $count Lines read"
Unit 11 configuring the bash shell – shell script
Ad

Recommended

Unit 6 bash shell
Unit 6 bash shell
root_fibo
 
Unit 5 vim an advanced text editor
Unit 5 vim an advanced text editor
root_fibo
 
Shell scripting
Shell scripting
simha.dev.lin
 
Shell scripting
Shell scripting
Manav Prasad
 
Quick start bash script
Quick start bash script
Simon Su
 
Unix - Shell Scripts
Unix - Shell Scripts
ananthimurugesan
 
Bash Shell Scripting
Bash Shell Scripting
Raghu nath
 
Chap06
Chap06
Dr.Ravi
 
Linux shell scripting
Linux shell scripting
Mohamed Abubakar Sittik A
 
SHELL PROGRAMMING
SHELL PROGRAMMING
jinal thakrar
 
Linux shell env
Linux shell env
Rahul Pola
 
Shell programming in ubuntu
Shell programming in ubuntu
baabtra.com - No. 1 supplier of quality freshers
 
Basics of shell programming
Basics of shell programming
Chandan Kumar Rana
 
Easiest way to start with Shell scripting
Easiest way to start with Shell scripting
Akshay Siwal
 
Shell Scripting
Shell Scripting
Gaurav Shinde
 
Shell programming
Shell programming
Moayad Moawiah
 
Shellscripting
Shellscripting
Narendra Sisodiya
 
Unix Basics
Unix Basics
Dr.Ravi
 
system management -shell programming by gaurav raikar
system management -shell programming by gaurav raikar
GauravRaikar3
 
Shell scripting
Shell scripting
Geeks Anonymes
 
Shell & Shell Script
Shell & Shell Script
Amit Ghosh
 
Unix shell scripting tutorial
Unix shell scripting tutorial
Prof. Dr. K. Adisesha
 
Pipes and filters
Pipes and filters
bhatvijetha
 
Unix Shell Scripting Basics
Unix Shell Scripting Basics
Sudharsan S
 
Talk Unix Shell Script
Talk Unix Shell Script
Dr.Ravi
 
Using Unix
Using Unix
Dr.Ravi
 
Unix Shell Scripting Basics
Unix Shell Scripting Basics
Dr.Ravi
 
Unix Shell Scripting
Unix Shell Scripting
Mustafa Qasim
 
Unit 4 user and group
Unit 4 user and group
root_fibo
 
Unit2 help
Unit2 help
root_fibo
 

More Related Content

What's hot (20)

Linux shell scripting
Linux shell scripting
Mohamed Abubakar Sittik A
 
SHELL PROGRAMMING
SHELL PROGRAMMING
jinal thakrar
 
Linux shell env
Linux shell env
Rahul Pola
 
Shell programming in ubuntu
Shell programming in ubuntu
baabtra.com - No. 1 supplier of quality freshers
 
Basics of shell programming
Basics of shell programming
Chandan Kumar Rana
 
Easiest way to start with Shell scripting
Easiest way to start with Shell scripting
Akshay Siwal
 
Shell Scripting
Shell Scripting
Gaurav Shinde
 
Shell programming
Shell programming
Moayad Moawiah
 
Shellscripting
Shellscripting
Narendra Sisodiya
 
Unix Basics
Unix Basics
Dr.Ravi
 
system management -shell programming by gaurav raikar
system management -shell programming by gaurav raikar
GauravRaikar3
 
Shell scripting
Shell scripting
Geeks Anonymes
 
Shell & Shell Script
Shell & Shell Script
Amit Ghosh
 
Unix shell scripting tutorial
Unix shell scripting tutorial
Prof. Dr. K. Adisesha
 
Pipes and filters
Pipes and filters
bhatvijetha
 
Unix Shell Scripting Basics
Unix Shell Scripting Basics
Sudharsan S
 
Talk Unix Shell Script
Talk Unix Shell Script
Dr.Ravi
 
Using Unix
Using Unix
Dr.Ravi
 
Unix Shell Scripting Basics
Unix Shell Scripting Basics
Dr.Ravi
 
Unix Shell Scripting
Unix Shell Scripting
Mustafa Qasim
 

Viewers also liked (6)

Unit 4 user and group
Unit 4 user and group
root_fibo
 
Unit2 help
Unit2 help
root_fibo
 
Unit 13 network client
Unit 13 network client
root_fibo
 
Unit 9 basic system configuration tools
Unit 9 basic system configuration tools
root_fibo
 
Administration 1 sw2012
Administration 1 sw2012
Lin Liyue
 
Unit 12 finding and processing files
Unit 12 finding and processing files
root_fibo
 
Unit 4 user and group
Unit 4 user and group
root_fibo
 
Unit 13 network client
Unit 13 network client
root_fibo
 
Unit 9 basic system configuration tools
Unit 9 basic system configuration tools
root_fibo
 
Administration 1 sw2012
Administration 1 sw2012
Lin Liyue
 
Unit 12 finding and processing files
Unit 12 finding and processing files
root_fibo
 
Ad

Similar to Unit 11 configuring the bash shell – shell script (20)

04 using and_configuring_bash
04 using and_configuring_bash
Shay Cohen
 
Bash Shell Scripting
Bash Shell Scripting
Raghu nath
 
Licão 09 variables and arrays v2
Licão 09 variables and arrays v2
Acácio Oliveira
 
Spsl by sasidhar 3 unit
Spsl by sasidhar 3 unit
Sasidhar Kothuru
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
mugeshmsd5
 
1 4 sp
1 4 sp
Bhargavi Bbv
 
Bash
Bash
KLabCyscorpions-TechBlog
 
Bash shell
Bash shell
xylas121
 
Shell scripting
Shell scripting
Mufaddal Haidermota
 
Shell scripting - Basic (PART -1)
Shell scripting - Basic (PART -1)
Chitrakshi Jaiswal
 
lec4.docx
lec4.docx
ismailaboshatra
 
Linux System Administration
Linux System Administration
Jayant Dalvi
 
Unixscripting
Unixscripting
Cesar Adolfo Balderas Guzman
 
Shell Script Linux
Shell Script Linux
Wellington Oliveira
 
BASH Guide Summary
BASH Guide Summary
Ohgyun Ahn
 
Shell scripting1232232312312312312312312
Shell scripting1232232312312312312312312
adnansalam11
 
Shell-Scripting-1.pdf
Shell-Scripting-1.pdf
aznabi
 
LINUX_admin_commands.pptx
LINUX_admin_commands.pptx
GuhanSenthil2
 
Linux Shell Scripting.pptx
Linux Shell Scripting.pptx
VaibhavJha46
 
34-shell-programming asda asda asd asd.ppt
34-shell-programming asda asda asd asd.ppt
poyotero
 
Ad

Recently uploaded (20)

UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
 
"Scaling in space and time with Temporal", Andriy Lupa.pdf
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
 
"Scaling in space and time with Temporal", Andriy Lupa.pdf
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 

Unit 11 configuring the bash shell – shell script

  • 1. RedHat Enterprise Linux Essential Unit 11: Configuring the Bash Shell – Shell script
  • 2. Objectives Upon completion of this unit, you should be able to:  Know how to use local and environment variables  Know how to inhibit variable expansion  Know how to create aliases  Understand how the shell parses a command line  Know how to configure startup files  Know how to handle input with the read command and positional parameters
  • 3. Bash Variables  Variables are named values  Useful for storing data or command output  Set with VARIABLE=VALUE  Referenced with $VARIABLE $ HI="Hello, and welcome to $(hostname)." $ echo $HI Hello, and welcome to stationX.
  • 4. Environment Variables  Variables are local to a single shell by default  Environment variables are inherited by child shells  Set with export VARIABLE=VALUE  Accessed by some programs for configuration
  • 5. Some Common Variables  Configuration variables  PS1: Appearance of the bash prompt  PATH: Directories to look for executables in  EDITOR: Default text editor  HISTFILESIZE: Number of commands in bash history  Information variables  HOME: User's home directory  EUID: User's effective UID
  • 6. example PS1 syntax: PS1='[display content]'  ! Display history number  # Display number of current command  $ Display $ or #  Display symbol  d Display current date  h Display hostname  s Display shell  t Display current time  u Display user  W Display home work current  w Display full path home work current PS1='t u@h s $'
  • 7. Aliases  Aliases let you create shortcuts to commands $ alias dir='ls -laht'  Use alias by itself to see all set aliases  Use alias followed by an alias name to see alias value $ alias dir alias dir='ls -laht'
  • 8. How bash Expands a Command Line  Split the line into words  Expand aliases  Expand curly-brace statements ({})  Expand tilde statements (~)  Expand variables ($)  Command-substituation ($() and ``)  Split the line into words again  Expand file globs (*, ?, [abc], etc)  Prepare I/O redirections (<, >)  Run the command!
  • 9. Preventing Expansion  Backslash ( ) makes the next character literal $ echo Your cost: $5.00 Your cost: $5.00  Quoting prevents expansion  Single quotes (') inhibit all expansion  Double quotes (") inhibit all expansion, except: • $ (dollar sign) - variable expansion • ` (backquotes) - command substitution • (backslash) - single character inhibition • ! (exclamation point) - history substitution
  • 10. Login vs non-login shells  Startup is configured differently for login and non-login shells  Login shells are:  Any shell created at login (includes X login)  su –  Non-login shells are:  su  graphical terminals  executed scripts  any other bash instances
  • 11. Bash startup tasks: profile  Stored in /etc/profile (global) and ~/.bash_profile (user)  Run for login shells only  Used for  Setting environment variables  Running commands (eg mail-checker script)
  • 12. Bash startup tasks: bashrc  Stored in /etc/bashrc (global) and ~/.bashrc (user)  Run for all shells  Used for  Setting local variables  Defining aliases
  • 13. Bash exit tasks  Stored in ~/.bash_logout (user)  Run when a login shell exits  Used for  Creating automatic backups  Cleaning out temporary files
  • 14. Scripting: Taking input with positional Parameters  Positional parameters are special variables that hold the command-line arguments to the script.  The positional parameters available are $1, $2, $3, etc. . These are normally assigned to more meaningful variable names to improve clarity.  $* holds all command-line arguments  $# holds the number of command-line arguments
  • 15. Scripting: Taking input with the read command  Use read to assign input values to one or more shell variables:  -p designates prompt to display  read reads from standard input and assigns one word to each variable  Any leftover words are assigned to the last variable  read -p "Enter a filename: " FILE #!/bin/bash read -p "Enter several values:" value1 value2 value3 echo "value1 is $value1" echo "value2 is $value2" echo "value3 is $value3"
  • 16. While  #!/bin/bash # SCRIPT: method1.sh # PURPOSE: Process a file line by line with PIPED while- read loop. FILENAME=$1 count=0 cat $FILENAME | while read LINE do let count++ echo "$count $LINE" done echo -e "nTotal $count Lines read"
  • 17. While with redirect  #!/bin/bash #SCRIPT: method2.sh #PURPOSE: Process a file line by line with redirected while-read loop. FILENAME=$1 count=0 while read LINE do let count++ echo "$count $LINE" done < $FILENAME echo -e "nTotal $count Lines read"