📜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


# Declaring variables

# Variable dereference using the '$' sign

Scope of Variables

Local variables

local name="Joe"

## Usage
# var scope example

name_change() {
	local name1="Edward"
	echo "Inside of this function, name1 is $name1 and name2 is $name2"

echo "Before the function call, name1 is $name1 and name2 is $name2"
echo "After the function call, name1 is $name1 and name2 is $name2"
kali@kali:~$ ./varscope.sh
Before the function call, name1 is John and name2 is Jason
Inside of this function, name1 is Edward and name2 is Jason
After the function call, name1 is John and name2 is Lucas

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

## Method 1
echo $user

## Method 2
echo $user

Reading user Input

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

kali@kali:~$ cat ./input.sh
echo "Hello there, would you like to learn how to hack: Y/N?"
read answer
echo "Your answer was $answer"
kali@kali:~$ chmod +x ./input.sh
kali@kali:~$ ./input.sh
Hello there, would you like to learn how to hack: Y/N?
Your answer was Y


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

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


If, Else, Elif Statements

General syntax

if [ <condition> ]
fi #block terminator

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

    # Example for Successful &&
    kali@kali:~$ user2=kali
    kali@kali:~$ grep $user2 /etc/passwd && echo "$user2 found!"
    kali found!
    # Failed &&
    kali@kali:~$ user2=bob
    kali@kali:~$ grep $user2 /etc/passwd && echo "$user2 found!"
  • || - The OR operator requires at-least 1 command, meaning, only if the first command failed, the next one will be executed.

    kali@kali:~$ grep $user2 /etc/passwd && echo "$user2 found!" || echo "$user2 not found!"
    bob not found!

Boolean Operators

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


For Loops

for var-name in <list>
<action to perform>

While Loops

while [ <some test> ]
<perform an action>
# while loops example
while [ $counter -le 10 ]
echo "10.11.1.$counter"

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}

    ## ways to generate a list of number or chars
    # 1st way
    seq 1 10
    #2nd way


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

# 1st format
function_name () {

# 2nd format 
function function_name {

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)

# function return value example
return_me() {
echo "Oh hello there, I'm returning a random value!"
return $RANDOM
echo "The previous function returned a value of $?"
kali@kali:~$ chmod +x ./funcrvalue.sh
kali@kali:~$ ./funcrvalue.sh
Oh hello there, I'm returning a random value!
The previous function returned a value of 198
  • 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