πŸ“œBash Scripting

Refernce page for bash scripting, summarized notes from PEN-200, These notes were written quite a whilte ago, so some inaccurasies might be expected.

A Bash script is a plain-text file that contains a series of commands that are executed as if they had been typed at a terminal prompt, sequentially.

Shebang -> #!

The shebang symbol is used to indicate the interpreter used to execute the script

e.g. #!/bin/bash

Bash Script Checklist

Bash Notes

  • Bash is case-sensitive

  • execute bash with ' -x ' flag to print extra debug info

  • Semi-colon ' ; ' is used as a command terminator

  • Single quotes acts different than double quotes

    • Double quotes allow the special meaning of these characters - $ , \ , `

    name='sahar shukrun'
    
    greeting1='Hello $name' # cannot resolve the variable and prints the var name as is
    --Hello $name
    
    greeting="Hello $name" # variable can be used even when quotes are in place
    --Hello sahar shukrun

Variables

Scope of Variables

Local variables

Command Substitution

A mechanism to save the output of a command to a variable; Two method are available:

  • Wrapping the command with: $( ) - Newer and preferred method

  • Wrapping the command with: ` - older and discouraged method (both method are implemented differently)

command substitution happens in a subshell and changes to variables in the subshell will not alter variables from the master process

Reading user Input

We can read the user input while the script is running by using 'read'

Read

  • -p - allows you to specify a prompt to the user

  • -s - makes the input silent (unseen on screen)

Arguments

Special Bash Variables

If, Else, Elif Statements

General syntax

Boolean operators & Command list

Command list

Command lists are a chain of commands that have a certain code flow according to the operators used

  • | - Pipe, is one such operator, a pipe will redirect the output of a command to the next command

  • && - The AND operator bind the commands it is being used by with an AND condition;

    If the first command is not successful, the second command will not be executed, it requires the first command to yield True, otherwise it will stop

  • || - The OR operator requires at-least 1 command, meaning, only if the first command failed, the next one will be executed.

Boolean Operators

The same && and || operator can be use like normal AND, OR in conditioning (if, else, etc)

Loops

For Loops

While Loops

Utility Commands

  • seq - sequence, seems to operate like python's 'range' function; 1st parameter is the first number on the list (inclusive), 2nd number is the end of the list (inclusive)

    • A sequence can also be created by using 'brace expansion' = {1..10}

Functions

Note: The parenthasis serve only as decoration, parameters cannot be used.

To get return values from the functions we can:

  • Use the $? global variable and receive an exit status (Zero for success, non-zero for failure)

  • Set a new global variable with the value we want to return

  • Use command substitution on the function and simulate a return value by assigning it to a new variable

Last updated

Was this helpful?