Bash Script Cheat Sheet

Jan 1, 0001 4 min read

On this page

Hello REDTM

#!/usr/bin/bash
#This is Comment

echo "Hello REDTM"
printf "Hello REDTM"

Save as hello.sh , give it execute permission chmod +x hello.sh and run ./hello.sh

Parameters

#!/usr/bin/bash
#This is Comment

echo -e "Hello $1"
printf "Hey, how is it goin $1?"
echo ""

$1 is the first parameters, second parameters should be $2 and so on.

Variables

Variables used to store data to use in future by referencing to the variable name! There are 4 type of variable we can use in bash. We can’t use Reserved word for variable names. Reserved words are:

if	
then	
elif	
else	
fi	
time
for	
in	
until	
while	
do	
done
case	
esac	
coproc	
select	
function
{	}	
[[	]]	
!
  1. Integer: These are numeric variables
  2. String: “Text based variable”
  3. Constant: “These variable Can’t be modified”
  4. Array: “Index of variable”

Integer Variable

#!/bin/bash

##Integer Variable
job=$3
name=$1

#Integer Variable
exp=5 

echo "$name having $exp+ experience in $job"
echo ""

String Variable

String variable is double quoted

#!/bin/bash

##String Variable
name="REDTM"
owner="Your name"

echo "The owner of $name is $owner"
echo "This is concated string: $name$owner"

Constant Variable

We can’t modify the variable. Two way to declare :

  1. Using declare -r
  2. Using readonly
##String Variable
readonly name="REDTM"

declare -r var="You"
var="Me"
readonly name2="Red Teaming"
name2="Blue Teaming"

echo $name2

echo $var

Execute in terminal and see the live result.

Array Variable

More details on Appropriate chapter

Command Substitutions

Two we can substitute command

  1. Using double backqoute “`Command_Here`”
  2. Using $(Command_here)
##Using backquote
passwd=`cat /etc/passwd`

##Using $ sign with first bracket
shadow="$(cat /etc/shadow)" #Without root, permission denied
issue="$(cat /etc/issue)"

#display contents
echo $passwd 
echo $shadow
echo $issue

Operators

Here is the some list of commonly used operators

Operator Example Description
= variable=1, var2="redtm" Assign Variable.
+,-,/,* var1=1;var1=2;var3=$((var1+var2)) Arithmetic Operator
+=, -=, *=, /= var+=1, var-=1, var*=2, var/=2 Increment by +1, Decrement by -1, Multiply by 2, divide by 2
& bitwish AND
! if [ ! -f $FILE] Not Operator
&& $condition1 && $condition2 And Operator
|| `$con1

Arrays

Bash support one-dimensional indexed. And the array index always start from 0. Way to declare an array:

  1. declare -a array_name
  2. Declare as variable but values in first brackets arr1=(One two three)
  3. Another way is declare -a array_name=(one two three)
#!/usr/bin/bash

#Array Variables
declare -a arr1
arr1=("One Two Three" "Two" "Three")
echo ${arr1[0]}

To reference the array we need to use ${array_name[number]}

Conditional

Conditional is a test. This mean, parametrically test for Fail and Success Status and divert the execution somewhere! Bash also has primary expression for conditional statements!

Primary Expression

Expression Description
-a file True if file exist
-b file True if file exist and a block file
-c file True if exist and it is a character special file
-d file True if it is a directory file
-e file True if file exist
-h file True if it is exist and a symbolic file
-k file True if it is exist and a sticky bit is set
-r file True if the file exist and Readable
-s file True if the file size is greater than 0
-w file True if File Exist and Writable
-x file True if File Exist and executable
-G file True if file exists and is owned by the effective group id.
-L file True if file exists and is a symbolic link.
-N file True if file exists and has been modified since it was last read.
-O file True if file exists and is owned by the effective user id.
-S file True if file exists and is a socket.
-z string True if the length of string is zero.
string1 == string2, string1 = string2 True if the strings are equal.
string1 != string2 True if the strings are not equal.
string1 < string2 True if string1 sorts before string2 lexicographically.
string1 > string2 True if string1 sorts after string2 lexicographically.
-eq Equal
-ne Not Equal
-le less than
-gt greater than

if

if/then/else

if/then/elif/else

Loop

Functions

Input/Output Redirections

Regular Expression

System Commands

It is not finished yet! 
Jobyer Ahmed
Written by
Jobyer Ahmed
Founder and Cybersecurity Professional
Jobyer Ahmed is the founder and cybersecurity professional at Bytium LLC. He works across offensive and defensive security, including penetration testing, red-team operations, and vulnerability management, with a focus on practical and audit-ready security improvements.