0% found this document useful (0 votes)
38 views

Bash-scripting-Cheat-Sheet-by-linuxsimply

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Bash-scripting-Cheat-Sheet-by-linuxsimply

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Bash Script

Cheat Sheet

Basic Syntax Variables


Shebang at the beginning of a script
#! /bin/bash var_name=val Assign a value to the specified variable
specifies the interpreter
#! /usr/bin/env Alternative shebang -using environment
$ var_name Access the value of the specified variable
bash variable
Variables with special bash script
Stores the number of argument passes character at the beginning must be
$# “$var_name”
to the Bash script quoted with double quotes or single
quotes
Variables that store the values passed as var_name=$(co Assign the output of a command to the
$1 , $2, $3
arguments to the Bash script mmand) specified variable
readonly Prevent the value of a specified variable
exit Exit from the Bash script
var_name=val to be modified
$HOME, $PATH,
CTRL + C Keyboard shortcut to stop Bash Few predefined environment variables
$USER etc.
Predefined varibles that stores the name
$ (command) Execute a command inside a subshell $0
of the script
Pause for a specified number of seconds, Predefined variables that stores the
sleep $#
minutes, hours or days number of command line arguments
Predefined variable that stores the exit
#?
status of the last executed command
Predefined variable that stores the
Comments $$
process ID of the current script
Predefined variable that stores the
Single line comment. The text comes
# $! proces ID of the last background
after it will not be executed
command
: <<' ' Multiple line comment unset var_name Delete a variable with specified name

Command Execution Input/Output


command_nam Directly execute the command with
read -p Prompt the user for information to enter
e specified name
`variable_name Older version of substituting the output command <
Redirect input from a file to a command
=command`` of the command to a specified variable input_file
command > Redirect the output of a command to a command 2>
Redirect standard error to a specified file
file_name specified file error_file
Redirect the output of a command to a
command >> command &> Redirect standard output and standard
specified command and append it with
file_name file_name error to a specified file
the existing content
command1 | Use the standard output of command1
command2 as the standard input of command2

Prepared By: Md Zahidul Islam Laku Copyright ©2023 linuxsimply.com| All rights reserved.
Bash Script
Cheat Sheet

Loops Conditional Statements


for variable in if [ condition ];
list; do Iterate over the list and execute code for then Test a condition and execute the then
# Code each element of the list #code clause if it is true
done fi
if [ condition ];
then
while condition;
#code Execute the then clause if the condition
do Execute code repeatedly as long as the
fi is true, otherwise execute the else
# Code condition is true
else clause
done
#code
fi
if [ condition1 ];
then
#code
until condition; Execute the then clause if the condition
elif [ condition2
do Execute code repeatedly until the is true or execute the elif clause if the
]; then
# Code condition becomes true condition is true, otherwise execute the
#code
done else clause
else
#code
fi
case variable in
pattern1)
#code
;;
pattern2)
select variable Execute code following each pattern if
#code
in list; do Execute code based on the choice that the variable matches the pattern
;;
# Code the variable takes from the list otherwise execute * if none of the
pattern3)
done patterns match
#code
;;
*)
;;
esac
Skip the current iteration of a loop and Returns 0 or 1 indicating whether the
continue test condition
continue with the next iteration condition is true or false
Terminate a loop based on certain
break
condition
Arithmetic Operations
Data Types + Addition
Integer or floating point values are
x=5 - Subtraction
treated as Number

Prepared By: Md Zahidul Islam Laku Copyright ©2023 linuxsimply.com| All rights reserved.
Bash Script
Cheat Sheet

Data Types Arithmetic Operations


is_valid=0 Boolean value represent False * Multiplication
is_valid=1 Boolean value represents True / Division
declare -a var Declare an indexed array % Modulus or remainder
declare -A var Declare an associated array ** Raise to a power
declare -i var Declare an integer variable ((i++)) Increment a variable
declare -r var Declare a read only variable ((i--)) Decrement a variable
declare -x var Declare an exported variable
var_name="" Absence of value or uninitialized variable Function

function_name(
array=("elemen
A collection of elements accessed using ){ Declare a function with specified
t1" "element2"
numerical indices # code function name
"element3"...)
}
declare -A
array1
array1["elemen A collection of elements accessed using Call a function with specified function
function_name
t1"]="value1" string indices name
array2["elemen
t2"]="value2"
Sequence of characters enclosed in
var="Hellow local
single or double quotes is treated as Declare a local variable inside a function
World" var_name=val
String
Exit a function and return a value of the
return
calling function
Boolean Operators
&& Logical AND operator Arithmetic Conditional Operators
Equals to mathematical < operator(less
|| Logical OR operator -lt
than)
Equals to mathematical >
! NOT equal to operator -gt
operator(greater than)
Equals to mathematical <= operator(less
-le
than equal)
Equals to mathematical >=
String Comaprison Opearators -ge
operator(greater than equal)
Equals to mathematical ==
= equal -eq
operator(equal)
Equals to mathematical != operator(not
!= not equal -ne
equal)
< less then
> greater then
-n str1 string str1 is not empty
-z str2 string str2 is empty

Prepared By: Md Zahidul Islam Laku Copyright ©2023 linuxsimply.com| All rights reserved.
Bash Script
Cheat Sheet

String Manipulation

concatenated=" Concatenate the variables set in str1 and


$str1 $str2" str2
Extracts a substring from n-th index to
substring=${str:
till the end of the string that stored in
n}
variable str
Extracts substring from 0-th index to 5-th
substring=${str:
index of the string that stored in variable
0:5}
str
Find the length of the string that stored
length=${#str}
in variable str
[[ $str == Returns True if the string stored in
*"World"* ]] variable str contains the word World
replaced=${str/ Replaces the first occurrence of 'World'
World/Universe with 'Universe' within the string stored
} in str variable
trimmed=${str# Trims leading whitespace of the string
trimmed=${trim Trims trailing whitespaces of the string
med%%*( )} stored in trimmed variable

Prepared By: Md Zahidul Islam Laku Copyright ©2023 linuxsimply.com| All rights reserved.

You might also like