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

More Related Content

PDF
Unit 6 bash shell
PDF
Unit 5 vim an advanced text editor
PPTX
Shell scripting
PDF
Shell scripting
PDF
Quick start bash script
PPTX
Unix - Shell Scripts
PPTX
Bash Shell Scripting
PPT
Chap06
Unit 6 bash shell
Unit 5 vim an advanced text editor
Shell scripting
Shell scripting
Quick start bash script
Unix - Shell Scripts
Bash Shell Scripting
Chap06

What's hot (20)

PPT
Linux shell scripting
PPTX
SHELL PROGRAMMING
PPTX
Linux shell env
ODP
PPTX
Basics of shell programming
PPTX
Easiest way to start with Shell scripting
PPT
Shell Scripting
PPT
Shell programming
ODP
Shellscripting
PPT
Unix Basics
PPTX
system management -shell programming by gaurav raikar
PDF
Shell scripting
PPTX
Shell & Shell Script
PDF
Unix shell scripting tutorial
PPTX
Pipes and filters
PPT
Unix Shell Scripting Basics
PPT
Talk Unix Shell Script
PPT
Using Unix
PPT
Unix Shell Scripting Basics
PDF
Unix Shell Scripting
Linux shell scripting
SHELL PROGRAMMING
Linux shell env
Basics of shell programming
Easiest way to start with Shell scripting
Shell Scripting
Shell programming
Shellscripting
Unix Basics
system management -shell programming by gaurav raikar
Shell scripting
Shell & Shell Script
Unix shell scripting tutorial
Pipes and filters
Unix Shell Scripting Basics
Talk Unix Shell Script
Using Unix
Unix Shell Scripting Basics
Unix Shell Scripting
Ad

Viewers also liked (6)

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

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

PPT
390aLecture05_12sp.ppt
PPT
Bash shell
PPTX
Licão 09 variables and arrays v2
PDF
003 scripting
PPTX
Bash Shell Scripting
PPT
Introduction to shell scripting ____.ppt
PPTX
First steps in C-Shell
PDF
BASH Guide Summary
PPTX
Customizing the unix environment
PPTX
KT on Bash Script.pptx
PPT
2-introduction_to_shell_scripting
PDF
Unleash your inner console cowboy
PPT
Spsl by sasidhar 3 unit
PPT
101 3.1 gnu and unix commands
PDF
PHP Programming and its Applications workshop
PPTX
Linux Shell Scripting
ODP
OpenGurukul : Language : Shell Scripting
PDF
php AND MYSQL _ppt.pdf
PDF
Php Tutorials for Beginners
PPTX
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
390aLecture05_12sp.ppt
Bash shell
Licão 09 variables and arrays v2
003 scripting
Bash Shell Scripting
Introduction to shell scripting ____.ppt
First steps in C-Shell
BASH Guide Summary
Customizing the unix environment
KT on Bash Script.pptx
2-introduction_to_shell_scripting
Unleash your inner console cowboy
Spsl by sasidhar 3 unit
101 3.1 gnu and unix commands
PHP Programming and its Applications workshop
Linux Shell Scripting
OpenGurukul : Language : Shell Scripting
php AND MYSQL _ppt.pdf
Php Tutorials for Beginners
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx

Recently uploaded (20)

PDF
Encapsulation theory and applications.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
MYSQL Presentation for SQL database connectivity
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Machine learning based COVID-19 study performance prediction
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Cloud computing and distributed systems.
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
Encapsulation theory and applications.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
MYSQL Presentation for SQL database connectivity
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Big Data Technologies - Introduction.pptx
Empathic Computing: Creating Shared Understanding
Mobile App Security Testing_ A Comprehensive Guide.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Building Integrated photovoltaic BIPV_UPV.pdf
Spectroscopy.pptx food analysis technology
Machine learning based COVID-19 study performance prediction
NewMind AI Weekly Chronicles - August'25 Week I
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
MIND Revenue Release Quarter 2 2025 Press Release
Cloud computing and distributed systems.
20250228 LYD VKU AI Blended-Learning.pptx
Advanced methodologies resolving dimensionality complications for autism neur...

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"