Bash Snippets
A curated list of basic Bash scripting snippets and examples
Contents
Shebang / Hashbang
Simple arithmetic
Logic, Control Flow
If-Then-Else Examples
Looping over a range
Looping over pattern-matched files
Multi-line looping example
Basic comparators
Basic comparators (double paranthesis form)
While loop
Files, Paths
Last file path component
Parent directory
Extracting, removing, or replacing file extension
Finding files
Finding files + coping with spaces for xargs
Image Files
Batch-converting images with sips
sipsStrings
String comparison
String concatenation
String adjustments
Pattern Matching, RegEx
More Pattern Matching
Script Arguments
Script arguments
Checking argument count
Current script path
Exit status of most recent command
Interactive
Reading input
Functions
Bash functions
Bash functions with arguments
Bash functions which 'return' a value
Piping, Command Substitution
Capturing output of another shell command
Printing, Stdout
(Re)printing on the same line
(Re)printing on the same line without leftover characters
Directing command stdout and stderr to /dev/null
Math
Logarithmic values via bc
HTTP, Network
Pretty JSON
curl
Testing time-to-first-byte
Download files using glob pattern
Compression
Create password-protected ZIP archive
Decompress Zip
git
Find & checkout branch by partial name
Useful Git CLI Utilities
Last updated
Was this helpful?