SlideShare a Scribd company logo
1 of 4
Download to read offline
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

Similar to Bash Cheat Sheet - SniferL4bs

What is the general format for a Try-Catch block Assume that amt l .docx
 What is the general format for a Try-Catch block  Assume that amt l .docx What is the general format for a Try-Catch block  Assume that amt l .docx
What is the general format for a Try-Catch block Assume that amt l .docxajoy21
 
String Interpolation in Scala
String Interpolation in ScalaString Interpolation in Scala
String Interpolation in ScalaKnoldus Inc.
 
Perl File Handling and Regex
Perl File Handling and RegexPerl File Handling and Regex
Perl File Handling and RegexJayant Parida
 
Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)Roy Zimmer
 
awkbash quick ref for Red hat Linux admin
awkbash quick ref for Red hat Linux adminawkbash quick ref for Red hat Linux admin
awkbash quick ref for Red hat Linux adminZoumanaDiomande1
 
Advanced linux chapter ix-shell script
Advanced linux chapter ix-shell scriptAdvanced linux chapter ix-shell script
Advanced linux chapter ix-shell scriptEliezer Moraes
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsYou Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsRoy Zimmer
 
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2Nihar Ranjan Paital
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionProf. Wim Van Criekinge
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlsana mateen
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionssana mateen
 
Regular expressionfunction
Regular expressionfunctionRegular expressionfunction
Regular expressionfunctionADARSH BHATT
 
shellScriptAlt.pptx
shellScriptAlt.pptxshellScriptAlt.pptx
shellScriptAlt.pptxNiladriDey18
 

Similar to Bash Cheat Sheet - SniferL4bs (20)

newperl5
newperl5newperl5
newperl5
 
newperl5
newperl5newperl5
newperl5
 
What is the general format for a Try-Catch block Assume that amt l .docx
 What is the general format for a Try-Catch block  Assume that amt l .docx What is the general format for a Try-Catch block  Assume that amt l .docx
What is the general format for a Try-Catch block Assume that amt l .docx
 
String Interpolation in Scala
String Interpolation in ScalaString Interpolation in Scala
String Interpolation in Scala
 
Perl File Handling and Regex
Perl File Handling and RegexPerl File Handling and Regex
Perl File Handling and Regex
 
Perl tutorial
Perl tutorialPerl tutorial
Perl tutorial
 
Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)
 
Perl slid
Perl slidPerl slid
Perl slid
 
awkbash quick ref for Red hat Linux admin
awkbash quick ref for Red hat Linux adminawkbash quick ref for Red hat Linux admin
awkbash quick ref for Red hat Linux admin
 
Advanced linux chapter ix-shell script
Advanced linux chapter ix-shell scriptAdvanced linux chapter ix-shell script
Advanced linux chapter ix-shell script
 
Ruby quick ref
Ruby quick refRuby quick ref
Ruby quick ref
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsYou Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager Needs
 
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2
 
RegexCat
RegexCatRegexCat
RegexCat
 
Spsl II unit
Spsl   II unitSpsl   II unit
Spsl II unit
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
 
Regular expressionfunction
Regular expressionfunctionRegular expressionfunction
Regular expressionfunction
 
shellScriptAlt.pptx
shellScriptAlt.pptxshellScriptAlt.pptx
shellScriptAlt.pptx
 

Recently uploaded

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
The Evolution of Money: Digital Transformation and CBDCs in Central Banking
The Evolution of Money: Digital Transformation and CBDCs in Central BankingThe Evolution of Money: Digital Transformation and CBDCs in Central Banking
The Evolution of Money: Digital Transformation and CBDCs in Central BankingSelcen Ozturkcan
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The Evolution of Money: Digital Transformation and CBDCs in Central Banking
The Evolution of Money: Digital Transformation and CBDCs in Central BankingThe Evolution of Money: Digital Transformation and CBDCs in Central Banking
The Evolution of Money: Digital Transformation and CBDCs in Central Banking
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

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