Bash Scripting Cheat Sheet

This Bash Scripting cheat sheet contains basic and intermediate Bash Shell scripts. This article is intended for both novices and working professionals to get a quick reference about different bash scripts.

Updated: 01 Jun, 23 by Susith Nonis 3 Min

List of content you will read in this article:

This Bash Scripting cheat sheet contains basic and intermediate Bash Shell scripts. This article is intended for both novices and working professionals to get a quick reference about different bash scripts.

Bash (Bourne Again Shell) is a command-line shell software. Brian Fox created it as an improved version of the Bourne Shell. It is a GNU open-source project. Let’s look at a few examples to get a quick reference to bash scripting.

For more information about bash scripts, you can check out our previous article here.

name="Linux"
echo "Hello $name!"

echo "Current directory is $(pwd)"
echo "Current directory is `pwd`"

if [[ "$condition1" ]]; then
  echo "Condition 1 satisfied"
elif [[ "$condition2" ]]; then
  echo "Condition 2 satisfied"
fi

if (( $x < $y )); then
  echo "$x is smaller than $y"
fi

if [[ "$s1" == "$s2" ]]  #if two strings are equal

# It is a comment

: '
This is
multi line
comment
'

read var
echo "You entered $var"

read a b
sum=$((a+b))
diff=$((a-b))
echo "Sum is $sum and difference is $diff"

get_value() {
  echo "2"
}
echo "Value is $(get_value)"

function myfunc() {
    echo "I am called"
}

NAME="Linux"
echo $NAME
echo "$NAME"
echo "${NAME}!"

arr=(1 2 3)
val=${arr[1]}
echo "Element at index 1 is $val"


arr=('X' 'Y' 'Z')
arr=("${arr[@]}" "A")    # Push an item
arr+=('A')                  # Push an item
unset arr[2]                         # Remove one item
arr=("${arr[@]}" "${arr2[@]}") # Concatenate with another array

echo ${arr[0]}           # Element #0
echo ${arr[-1]}          # Last element
echo ${#arr[@]}          # Number of elements

for i in "${arr[@]}"; do   #iterate through the array
  echo $i
done

VAR1='Hello Linux'
echo  $VAR1

name="Linux"
echo ${name}
echo ${name:0:2}    #=> "Li" (slicing the indices)
echo ${name::2}     #=> "Li" (slicing the indices)
echo ${name::-1}    #=> "Lin" (slicing the indices)
echo ${name:(-1)}   #=> "x" (slicing the from right)
echo ${name:(-2):1} #=> "u" (slicing the indices from right)

STR="LINUX"
echo ${STR,,}  #=> "linux" (all become lowercase letters)

STR="linux"
echo ${STR^^}  #=> "LINUX" (all become uppercase uppercase)

${#str}  #length of $str

for ((i = 0 ; i < 10 ; i++)); do
  echo $i
done

while true; do
  ···
done

for i in {0..10}; do
    echo "We are on $i"
done

for i in {0..10..2}; do  #loop using step size of 2
    echo "We are on $i"
done

apt-get update
apt-get -y upgrade
apt-get -y autoremove
apt-get autoclean

Susith Nonis

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.

user monovm

Eldred Jakubowski

2024, Aug, 24

This Bash Scripting cheat sheet is a fantastic resource for anyone looking to brush up on their skills or learn something new. It covers a wide range of topics, from basic variable creation to more advanced concepts like loops and functions. The examples provided are clear and easy to follow, making it a valuable reference for both beginners and seasoned professionals. Definitely bookmarking this for future projects.

user monovm

Dr. Amalia Walsh

2025, Aug, 25

This cheat sheet is an absolute gem! It’s compact yet incredibly informative, making it perfect for both beginners who are just diving into bash scripting and seasoned professionals who need a quick reference. The examples are crystal clear and cover a wide range of topics, from basic variable printing to advanced loops and system maintenance. Definitely bookmarking this page, and I appreciate the link to more detailed articles for a deeper dive into the world of Bash. Thanks for sharing this resource!