programming

Bash Script Cheat Sheet

Bash Script Cheat Sheet

Dec 19, 2023·5 min read·By Jobyer Ahmed

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

OperatorExampleDescription
=variable=1, var2="redtm"Assign Variable.
+,-,/,*var1=1;var1=2;var3=$((var1+var2))Arithmetic Operator
+=, -=, *=, /=var+=1, var-=1, var*=2, var/=2Increment by +1, Decrement by -1, Multiply by 2, divide by 2
&bitwish AND
!if [ ! -f $FILE]Not Operator
&&$condition1 && $condition2And 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

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

if

if/then/else

if/then/elif/else

Loop

Functions

Input/Output Redirections

Regular Expression

System Commands

It is not finished yet!