Skip to main content

Bash

Declare Var

variable=10
str=Hello World # wrong
str="Hello World" # correct
str='Hello World' # correct

Single Quote: interprets every enclosed character literally

Double Quote: all characters are viewed literally except "$", "`", and "" meaning variables will be expanded in an initial substitution pass on the enclosed text

$(): Command Substitution, set the value of the variable to the result of a command or program

``: Command Substitution, The backtick method is older and typically discouraged as there are differences in how the two methods of command substitution behave

Variable NameDescription
$0The name of the Bash Script
11-9The first 9 arguments to the Bash Script
$#Number of arguments passed to the Bash Script
$@All arguments passed to the Bash Script
$?The exit status of the most recently run process
$$The process ID of the current script
$USERThe username of the user running the script
$HOSTNAMEThe hostname of the machine
$RANDOMA random number
$LINENOThe current line number in the script

Read User Input

echo "Hello there, would you like to learn how to hack: Y/N?"
read answer
echo "Your answer was $answer"
read -p 'Username: ' username 	# -p allows us to specify a prompt
read -sp 'Password: ' password # -s makes the user input silent
echo "Thanks, your creds are as follows: " $username " and " $password

Conditional Statements

if [ <some test> ] 		# [ ] square bracket can be replaced by test
then
<perform action>
elif [ <some test> ]
then
<perform different action>
else
<perform yet another different action>
fi
OperatorDesciption: Expression True of...
!EXPRESSIONThe EXPRESSION is false
-n STRINGSTRING length is greater than zero
-z STRINGThe length of STRING is zero (empty)
STRING1 != STRING2STRING1 is not equal to STRING2
STRING1 = STRING2STRING1 is equal to STRING2
INTEGER1 -eq INTEGER2Equal
INTEGER1 -ne INTEGER2Not Equal
INTEGER1 -gt INTEGER2Greater Than
INTEGER1 -lt INTEGER2Less Than
INTEGER1 -ge INTEGER2>=
INTEGER1 -le INTEGER2<=
-d FILEFILE exists and is a dir
-e FILEFILE exists
-r FILEFILE exists and has read permission
-s FILEFILE exists and it is not empty
-w FILEFILE exists and has write permission
-x FILEFILE exists and has execute permission

Logic Operator

grep $USER /etc/passwd && echo "$USER found!"		# the second half is executed iff the first half is True
grep $user2 /etc/passwd && echo "$user2 found!" || echo "$user2 not found !" # when grep does not find a matching line and returns False, the second echo command after the OR (||) operator is executed instead.

Loops

for var-name in <list>
do
<action to perform>
done
for ip in $(seq 1 10); do echo 10.11.1.$ip; done
for i in {1..10}; do echo 10.11.1.$i;done
while [ <some test> ]
d
<perform an action>
done
counter=1
while [ $counter -lt 10 ]
do
echo "10.11.1.$counter"
((counter++)) # The ((counter++)) line uses the double-parenthesis (( )) construct to perform arithmetic expansion and evaluation at the same time
done

Functions

# format 1
function func_name {
...
}

# format 2
func_name () {
...
}

# function example
print_me () {
echo "You have been printed!"
}
print_me

# function example 2
pass_arg() {
echo "Today's random number is: $1" # $1 is the passed in argument
}

pass_arg $RANDOM

Scope

By default, a variable has a global scope, meaning it can be accessed throughout the entire script.

A local variable can only be seen within the function, block of code, or subshell in which it is defined.

local name="Joe"