Bash Tutorial

Bash Scripting Tutorial

Part 1 - Bash Shell Scripting Basics




Introduction and Basics:

  • Introduction
  • Bash Introduction. What is Bash? Bourne Again Shell.
  • What is a Shell?
  • Internal and External Commands, Type command, Bash Configuration Files - Startup Files.
  • Bash Shell Command Interpreter
  • Bash Command Interpreter.

    Filename Wildcard Expansion.

    Variable Expansion.

    Pre Defined Variables.

    Command Substitution.

    Quotes: Single Quotes and Double Quotes explained.

    Backslash - Escape Character.
  • Output Streams
  • Standard Input, Standard Output and Standard Error explained.

    Redirection and Pipes.
  • Working with Variables
  • Assigning values to variables

    Using the read command

    IFS - Internal Field Separator

    env, set, export and unset commands.

    Using printf to format output.
  • Bash Shell Variables
  • What are Shell Variables?

    Special Shell Variables.

    Using the shift command.

    Using the eval command.
  • Declare
  • Bash Declare statement.

    Changing properties of a variable with declare.

    Displaying variable properties with declare -p.
  • Bash Scripting Basic Structure
  • Basic Structure of a script.

    Using comments.

    Handling errors - Custom Error logs.

    Execute Permissions using chmod.

    $PATH explained.

    Adding new paths to $PATH.



Part 2 - Bash Shell Scripting


Functions and Blocks of Code:

  • Debugging and Return Codes
  • Debugging Bash Scripts. Setting return codes.
  • Functions
  • What are functions?

    Defining functions.

    Working with functions.

    Calling functions from within scripts.

    Creating a functions library.
  • Here Documents
  • What are Here Documents?

    Here document examples.
  • Here Strings
  • What are Here Strings?

    Here String examples.



Part 3 - Bash Shell Scripting Basic Structure


Controlling the flow with conditional processing:

  • Bash Scripting if statement
  • Conditional Processing

    "if..then..fi"

    "if..then..else..fi"

    "if ..then..elif..then..else..fi

    Testing files and objects with "if" statements.
  • Comparing Strings
  • String Comparison tests.

    Using Single Brackets.

    Using Double Brackets.

    Using Regular Expressions with Double Brackets.
  • Bash Scripting Substrings
  • What are Substrings?

    Calculate length of a string.

    Extracting a substring from a variable.

    Replacing matches within substrings.
  • Comparing Numbers
  • Numerical Operators.

    Integer Operators.

    Testing numeric values and working with numbers.
  • Boolean Operators
  • Logical Boolean Operators.

    Logical Negation, Logical AND, Logical OR.

    Combining Logical Operators.
  • Bash Scripting Case Statements
  • Using Case statements to simplify logic within a script.

    Pattern matching with case statements.

    Mixed case pattern matching using the case statement.



Part 4 - Using Loops within Bash Shell Scripts


Loops:



Part 5 - Number and Arithmetic


Making calculations in Bash:

  • Number and Arithmetic
  • Mathematical Operations within Bash.

    Arithmetic Operators.

    Addition, Subtraction, Division and Multiplication.

    Using Modulo command - modulo example.

    Exponentiation - exponentiation example.

    BC - Bash built in arbitrary precision calculator.

    Mathematical Functions.

    Invoking the Math library.



Part 6 - Arrays


Working with arrays:

  • Working with arrays
  • What is an array?

    Array assignment.

    Deleting arrays with the unset command.

    Bash array example scripts.



Part 7 - Interactive Scripts


Interactive Scripts:

  • Getting User Input
  • Create interactive scripts.

    Using the read command.

    Simple Menus with echo and read commands.
  • Select Statement
  • Using Select.

    Create interactive menus with select statement.

    Combining select and case statements to create menus.