SlideShare a Scribd company logo
Bash Cheat Sheet
Strings:
Operators
${varname:-word}

If varname exists and isn't null, return its value; otherwise return
word.

${varname:=word}

If varname exists and isn't null, return its value; otherwise set it to
word and then return its value.

${varname:?message}

If varname exists and isn't null, return its value; otherwise print
varname: followed by message, and abort the current command
or script.

${varname:+word}

If varname exists and isn't null, return word; otherwise return null.

${varname:offset:length} Performs substring expansion. It returns the substring of
$varname starting at offset and up to length characters. If
length is omitted substring expansion starts at offset and
continues to end of $varname

Pattern Matching
${variable#pattern}

If the pattern matches the beginning of the variable's value,
delete the shortest part that matches and return the rest.

${variable##pattern}

If the pattern matches the beginning of the variable's value,
delete the longest part that matches and return the rest.

${variable%pattern}

If the pattern matches the end of the variable's value, delete the
shortest part that matches and return the rest.

${variable%%pattern}

If the pattern matches the end of the variable's value, delete the
longest part that matches and return the rest.

${variable/pattern/string} The longest match to pattern in variable is replaced by string.
${variable//pattern/string} All matches to pattern in variable are replaced by string
Condition Tests
Example: [ condition ]
Operator

True if

string1 = string 2

string1 matches string2

string1 != string 2

string1 does not match string2

string1 == string2

string1 is equal to string2

string1 !== string2

string1 is not equal to string2

string1 < string2

string1 is less than string2

string1 > string2

string1 is greater than string2

-n string1

string1 is not null

-z string1

string1 is null

&&

Logical AND

||

Logical OR

File Condition T
ests
Example: [ condition ]
Operator

True If

-a file

file exists

-d file

file exists and is a directory

-f file

file exists and is a regular file (e.g. is not a directory)

-r file

You have read permission on file. Can also be used with -w, -x for
write, and execute permissions respectively.

-s file

file exists and is not empty

file1 -nt file2

file1 is newer than file2

file1 -ot file2

file1 is older than file2
Integers
Setting Variables
declare can be used with options to set variables. For example: declare -i var1
-a
-i
-r

The variables are treated as arrays
The variables are treated as integers
Makes the variable read only

Operators
Use with double parenthesis. For example: echo $((var1++))
Operator

Meaning

++

Increment by one (prefix and postfix)

--

Decrement by one (prefix and postfix)

+, -, *, /

Add, subtract, multiply, and divide respectively

%

Remainder of division

**

Exponentiation

Conditionals
Integer variables take different conditionals than strings. For example: [ 3 -gt 2 ]
Operator

Meaning

-lt

Less than

-gt

Greater than

-le

Less than or equal to

-ge

Greater than or equal to

-eq

Equal to

-ne

Not equal to

Alternately, the regular operators can be called out, provided the expression is surrounded by
double parenthesis: eg: echo $(( (3 > 2) && (4 <= 1) ))
Arrays
Storing
array1[2]=value
Will store value as the second element of array1
array1=([2]=value [0]=..) Will store value as the second element of array1...
array1=(value value)
Will store values in array1 in the order they are entered

Recalling
${array1[0]}
${array1[*]}
${!array1[*]}
${#array1[1]}
${#array1[*]}

will return element 0 of array1
will return all elements of array1
will return occupied array1
will return the length of element 1
will return the length of the array1

Loops
If

For
if condition
then
statements
[elif condition
then
statements]
[else
statements]
fi

Select
select name [in list]
do
statements that
can use $name
done

for name [in list]
do
statements that can use
$name
done

While
while condition
do
statements
done

Case
case expression in
pattern1 )
statements ;;
pattern2 )
statements ;;
esac

More Related Content

PPT
Textpad and Regular Expressions
PPTX
Javascript ch2
DOC
Huffman coding01
PDF
Regex startup
PDF
Enter Sandbox: Android Sandbox Comparison
PDF
Kali tools list with short description
PDF
Wfuzz for Penetration Testers
PPTX
Client side attacks
Textpad and Regular Expressions
Javascript ch2
Huffman coding01
Regex startup
Enter Sandbox: Android Sandbox Comparison
Kali tools list with short description
Wfuzz for Penetration Testers
Client side attacks

Similar to Bash Cheat Sheet - SniferL4bs (20)

PDF
newperl5
PDF
newperl5
DOCX
What is the general format for a Try-Catch block Assume that amt l .docx
ODP
String Interpolation in Scala
PPT
Perl File Handling and Regex
PPT
Perl tutorial
PPT
Plunging Into Perl While Avoiding the Deep End (mostly)
PPTX
Perl slid
PDF
awkbash quick ref for Red hat Linux admin
PPT
Advanced linux chapter ix-shell script
PDF
Ruby quick ref
PPT
You Can Do It! Start Using Perl to Handle Your Voyager Needs
PPS
UNIX - Class5 - Advance Shell Scripting-P2
PDF
RegexCat
PPT
Spsl II unit
PPT
Bioinformatica 06-10-2011-p2 introduction
PPTX
Strings,patterns and regular expressions in perl
PPTX
Unit 1-strings,patterns and regular expressions
DOCX
Regular expressionfunction
PPTX
shellScriptAlt.pptx
newperl5
newperl5
What is the general format for a Try-Catch block Assume that amt l .docx
String Interpolation in Scala
Perl File Handling and Regex
Perl tutorial
Plunging Into Perl While Avoiding the Deep End (mostly)
Perl slid
awkbash quick ref for Red hat Linux admin
Advanced linux chapter ix-shell script
Ruby quick ref
You Can Do It! Start Using Perl to Handle Your Voyager Needs
UNIX - Class5 - Advance Shell Scripting-P2
RegexCat
Spsl II unit
Bioinformatica 06-10-2011-p2 introduction
Strings,patterns and regular expressions in perl
Unit 1-strings,patterns and regular expressions
Regular expressionfunction
shellScriptAlt.pptx
Ad

Recently uploaded (20)

PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Mushroom cultivation and it's methods.pdf
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Tartificialntelligence_presentation.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Empathic Computing: Creating Shared Understanding
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Machine learning based COVID-19 study performance prediction
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
August Patch Tuesday
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
A comparative analysis of optical character recognition models for extracting...
Unlocking AI with Model Context Protocol (MCP)
Mushroom cultivation and it's methods.pdf
SOPHOS-XG Firewall Administrator PPT.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Network Security Unit 5.pdf for BCA BBA.
Tartificialntelligence_presentation.pptx
Spectroscopy.pptx food analysis technology
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation_ Review paper, used for researhc scholars
Empathic Computing: Creating Shared Understanding
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Machine learning based COVID-19 study performance prediction
Accuracy of neural networks in brain wave diagnosis of schizophrenia
August Patch Tuesday
Ad

Bash Cheat Sheet - SniferL4bs

  • 1. Bash Cheat Sheet Strings: Operators ${varname:-word} If varname exists and isn't null, return its value; otherwise return word. ${varname:=word} If varname exists and isn't null, return its value; otherwise set it to word and then return its value. ${varname:?message} If varname exists and isn't null, return its value; otherwise print varname: followed by message, and abort the current command or script. ${varname:+word} If varname exists and isn't null, return word; otherwise return null. ${varname:offset:length} Performs substring expansion. It returns the substring of $varname starting at offset and up to length characters. If length is omitted substring expansion starts at offset and continues to end of $varname Pattern Matching ${variable#pattern} If the pattern matches the beginning of the variable's value, delete the shortest part that matches and return the rest. ${variable##pattern} If the pattern matches the beginning of the variable's value, delete the longest part that matches and return the rest. ${variable%pattern} If the pattern matches the end of the variable's value, delete the shortest part that matches and return the rest. ${variable%%pattern} If the pattern matches the end of the variable's value, delete the longest part that matches and return the rest. ${variable/pattern/string} The longest match to pattern in variable is replaced by string. ${variable//pattern/string} All matches to pattern in variable are replaced by string
  • 2. Condition Tests Example: [ condition ] Operator True if string1 = string 2 string1 matches string2 string1 != string 2 string1 does not match string2 string1 == string2 string1 is equal to string2 string1 !== string2 string1 is not equal to string2 string1 < string2 string1 is less than string2 string1 > string2 string1 is greater than string2 -n string1 string1 is not null -z string1 string1 is null && Logical AND || Logical OR File Condition T ests Example: [ condition ] Operator True If -a file file exists -d file file exists and is a directory -f file file exists and is a regular file (e.g. is not a directory) -r file You have read permission on file. Can also be used with -w, -x for write, and execute permissions respectively. -s file file exists and is not empty file1 -nt file2 file1 is newer than file2 file1 -ot file2 file1 is older than file2
  • 3. Integers Setting Variables declare can be used with options to set variables. For example: declare -i var1 -a -i -r The variables are treated as arrays The variables are treated as integers Makes the variable read only Operators Use with double parenthesis. For example: echo $((var1++)) Operator Meaning ++ Increment by one (prefix and postfix) -- Decrement by one (prefix and postfix) +, -, *, / Add, subtract, multiply, and divide respectively % Remainder of division ** Exponentiation Conditionals Integer variables take different conditionals than strings. For example: [ 3 -gt 2 ] Operator Meaning -lt Less than -gt Greater than -le Less than or equal to -ge Greater than or equal to -eq Equal to -ne Not equal to Alternately, the regular operators can be called out, provided the expression is surrounded by double parenthesis: eg: echo $(( (3 > 2) && (4 <= 1) ))
  • 4. Arrays Storing array1[2]=value Will store value as the second element of array1 array1=([2]=value [0]=..) Will store value as the second element of array1... array1=(value value) Will store values in array1 in the order they are entered Recalling ${array1[0]} ${array1[*]} ${!array1[*]} ${#array1[1]} ${#array1[*]} will return element 0 of array1 will return all elements of array1 will return occupied array1 will return the length of element 1 will return the length of the array1 Loops If For if condition then statements [elif condition then statements] [else statements] fi Select select name [in list] do statements that can use $name done for name [in list] do statements that can use $name done While while condition do statements done Case case expression in pattern1 ) statements ;; pattern2 ) statements ;; esac