SlideShare une entreprise Scribd logo
1  sur  18
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Tendances (20)

Linux shell scripting
Linux shell scriptingLinux shell scripting
Linux shell scripting
 
SHELL PROGRAMMING
SHELL PROGRAMMINGSHELL PROGRAMMING
SHELL PROGRAMMING
 
Linux shell env
Linux shell envLinux shell env
Linux shell env
 
Shell programming in ubuntu
Shell programming in ubuntuShell programming in ubuntu
Shell programming in ubuntu
 
Basics of shell programming
Basics of shell programmingBasics of shell programming
Basics of shell programming
 
Easiest way to start with Shell scripting
Easiest way to start with Shell scriptingEasiest way to start with Shell scripting
Easiest way to start with Shell scripting
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Shell programming
Shell programmingShell programming
Shell programming
 
Shellscripting
ShellscriptingShellscripting
Shellscripting
 
Unix Basics
Unix BasicsUnix Basics
Unix Basics
 
system management -shell programming by gaurav raikar
system management -shell programming by gaurav raikarsystem management -shell programming by gaurav raikar
system management -shell programming by gaurav raikar
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script
 
Unix shell scripting tutorial
Unix shell scripting tutorialUnix shell scripting tutorial
Unix shell scripting tutorial
 
Pipes and filters
Pipes and filtersPipes and filters
Pipes and filters
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell Script
 
Using Unix
Using UnixUsing Unix
Using Unix
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
Unix Shell Scripting
Unix Shell ScriptingUnix Shell Scripting
Unix Shell Scripting
 

En vedette

Unit 4 user and group
Unit 4 user and groupUnit 4 user and group
Unit 4 user and grouproot_fibo
 
Unit 13 network client
Unit 13 network clientUnit 13 network client
Unit 13 network clientroot_fibo
 
Unit 9 basic system configuration tools
Unit 9 basic system configuration toolsUnit 9 basic system configuration tools
Unit 9 basic system configuration toolsroot_fibo
 
Administration 1 sw2012
Administration 1 sw2012Administration 1 sw2012
Administration 1 sw2012Lin Liyue
 
Unit 12 finding and processing files
Unit 12 finding and processing filesUnit 12 finding and processing files
Unit 12 finding and processing filesroot_fibo
 

En vedette (6)

Unit 4 user and group
Unit 4 user and groupUnit 4 user and group
Unit 4 user and group
 
Unit2 help
Unit2 helpUnit2 help
Unit2 help
 
Unit 13 network client
Unit 13 network clientUnit 13 network client
Unit 13 network client
 
Unit 9 basic system configuration tools
Unit 9 basic system configuration toolsUnit 9 basic system configuration tools
Unit 9 basic system configuration tools
 
Administration 1 sw2012
Administration 1 sw2012Administration 1 sw2012
Administration 1 sw2012
 
Unit 12 finding and processing files
Unit 12 finding and processing filesUnit 12 finding and processing files
Unit 12 finding and processing files
 

Similaire à Unit 11 configuring the bash shell – shell script

390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.pptmugeshmsd5
 
Bash shell
Bash shellBash shell
Bash shellxylas121
 
Licão 09 variables and arrays v2
Licão 09 variables and arrays v2Licão 09 variables and arrays v2
Licão 09 variables and arrays v2Acácio Oliveira
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide SummaryOhgyun Ahn
 
Customizing the unix environment
Customizing the unix environmentCustomizing the unix environment
Customizing the unix environmentDr. Girish GS
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scriptingerbipulkumar
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboyKenneth Geisshirt
 
101 3.1 gnu and unix commands
101 3.1 gnu and unix commands101 3.1 gnu and unix commands
101 3.1 gnu and unix commandsAcácio Oliveira
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell ScriptingRaghu nath
 
OpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell ScriptingOpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell ScriptingOpen Gurukul
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on LinuxTushar B Kute
 
UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1Nihar Ranjan Paital
 
COMELEC III - Bash unit 1
COMELEC III - Bash unit 1COMELEC III - Bash unit 1
COMELEC III - Bash unit 1Binsent Ribera
 

Similaire à Unit 11 configuring the bash shell – shell script (20)

390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
 
Bash shell
Bash shellBash shell
Bash shell
 
Licão 09 variables and arrays v2
Licão 09 variables and arrays v2Licão 09 variables and arrays v2
Licão 09 variables and arrays v2
 
003 scripting
003 scripting003 scripting
003 scripting
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
First steps in C-Shell
First steps in C-ShellFirst steps in C-Shell
First steps in C-Shell
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide Summary
 
Customizing the unix environment
Customizing the unix environmentCustomizing the unix environment
Customizing the unix environment
 
KT on Bash Script.pptx
KT on Bash Script.pptxKT on Bash Script.pptx
KT on Bash Script.pptx
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboy
 
Spsl by sasidhar 3 unit
Spsl by sasidhar  3 unitSpsl by sasidhar  3 unit
Spsl by sasidhar 3 unit
 
101 3.1 gnu and unix commands
101 3.1 gnu and unix commands101 3.1 gnu and unix commands
101 3.1 gnu and unix commands
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
 
OpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell ScriptingOpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell Scripting
 
php AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdfphp AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdf
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on Linux
 
UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1
 
COMELEC III - Bash unit 1
COMELEC III - Bash unit 1COMELEC III - Bash unit 1
COMELEC III - Bash unit 1
 

Dernier

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 

Dernier (20)

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

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"