Bash Snippets
A curated list of basic Bash scripting snippets and examples
Last updated
Was this helpful?
A curated list of basic Bash scripting snippets and examples
Last updated
Was this helpful?
ąøŖąø±ąøąøąø„ąø±ąøąø©ąøą¹ "shebang" หรืภ"hashbang" (ą¹ąøą¹ąøąø°ąøąø“ąø¢ąø”ą¹ąø£ąøµąø¢ąø shebang) ąøąø·ąøąøŖąø±ąøąø„ąø±ąøąø©ąøą¹ #!
ąøąøµą¹ąøąø¢ąø¹ą¹ąøąøµą¹ąøą¹ąøąøąøąøąøŖąøąø£ąø“ąøąøą¹ ąøąø²ąø”ąøą¹ąø§ąø¢ą¹ąøŖą¹ąøąøąø²ąø (path) ą¹ąøąø¢ąø±ąøą¹ąøąø£ą¹ąøąø£ąø”ąøąøµą¹ą¹ąøą¹ą¹ąøąøąø²ąø£ąø£ąø±ąøąøŖąøąø£ąø“ąøąøą¹ąøąø±ą¹ąø (interpreter) ą¹ąø§ąø„ąø²ąøąøµą¹ąø£ąø±ąøąøŖąøąø£ąø“ąøąøą¹ąøą¹ąø§ąø¢ąøąø³ąøŖąø±ą¹ąøą¹ąøą¹ąøąø Unix/Linux ąø£ąø°ąøąøąøąø°ąøą¹ąø²ąø shebang ą¹ąøąø·ą¹ąøąøąø¹ąø§ą¹ąø²ąøąø§ąø£ą¹ąøą¹ą¹ąøąø£ą¹ąøąø£ąø”ąøąø°ą¹ąø£ą¹ąøąøąø²ąø£ąø£ąø±ąøąøŖąøąø£ąø“ąøąøą¹ąøąø±ą¹ąø
Shebang ąø”ąøµąøąø§ąø²ąø”ąøŖąø³ąøąø±ąøąøŖąø³ąø«ąø£ąø±ąøąø£ąø°ąøąøąøąøąø“ąøąø±ąøąø“ąøąø²ąø£ą¹ąøąø Unix/Linux ą¹ąø”ąø·ą¹ąøąø£ąø°ąøąøą¹ąø«ą¹ąøąøŖąøąø£ąø“ąøąøą¹ąøąøµą¹ą¹ąø£ąø“ą¹ąø”ąøą¹ąøąøą¹ąø§ąø¢ #!
ąø£ąø°ąøąøąøąø°ąø£ąø±ąøąøŖąøąø£ąø“ąøąøą¹ąøąø±ą¹ąøą¹ąøąø¢ą¹ąøą¹ interpreter ąøąøµą¹ąø£ąø°ąøąøøą¹ąø§ą¹ ąøą¹ąø²ąøŖąøąø£ąø“ąøąøą¹ą¹ąø”ą¹ąø”ąøµ shebang ąøąøøąøąøąø°ąøą¹ąøąøąø£ąø±ąøą¹ąøąø¢ąø£ąø°ąøąøø interpreter ą¹ąøąø¢ąøąø£ąø ą¹ąøą¹ąø bash myscript.sh
หรืภpython3 myscript.py
Use $(( ))
.
basename "/My/path/to/file.txt"
Result: file.txt
dirname "/My/path/to/file.txt"
Result: /My/path/to
Result:
Basic matching by name:
find . -name "*.jpg"
Combining queries with -and
/ -or
:
find . -name "*.swift" -or -name "*.m"
Won't work if paths contain spaces etc.:
find . -name "*.swift" | xargs wc -l
Works:
find . -name "*.swift" | sed 's/ /\\ /g' | xargs wc -l
Also works:
find . -name "*.swift" -print0 | xargs -0 wc -l
sips
Components via cut
Character replacement
Uppercasing / lowercasing
Removing characters
echo "Hello" | tr -d "el" #Result: Ho
Substituion via sed:
echo "The quick brown fox" | sed 's/brown/red/' #Result: The quick red fox
Note that sed, by default, matches once per line and is case sensitive:
echo "Hello, hello, hello" | sed 's/hello/goodbye/' #Result: Hello, goodbye, hello
Pattern-matched replacement:
Below are several pattern matching examples which build progressively upon each other. Before this, however, the below sed
syntax deserves a special mention due to its versatility. By far the most common pattern matching task I encounter is to match a pattern and extract a portion. There are a variety of ways to do this, however the sed
command below is quite handy:
sed -n "s| <regex> \( <regex> \) |\1|p"
The above command can be used to match any pattern and extract some or all of the text as the output.
The -n
flag suppresses sed's default behavior to print every input line
The s
flag indicates a substitution operation
The first block of text is the pattern to match, and we also specify a capture group via the escaped parantheses \(
and \)
The p
flag instructs sed to print only the text which is substituted
The \1
for the substitution will match the text in our capture group (the parentheses)
Examples:
echo "The quick brown fox jumps over the lazy dog." | sed -n "s|.*the \(.*\) dog.*|\1|p"
Result: lazy
Comment: note the .*
at the very beginning and end of the pattern, which captures all of text before or after the matched portion. This ensures that nothing except the match will be part of the substituion & output.
echo "The quick brown fox jumps over the lazy dog." | sed -n "s|.*\(the .* dog\).*|\1|p"
Result: the lazy dog
echo "The quick brown fox jumps over the lazy dog." | sed -n "s|.*the \(.*\) dog.*|What is the dog? \1|p"
Result: What is the dog? lazy
echo "The quick brown fox jumps over the lazy dog." | sed -n "s|the \(.*\) dog|something else|p"
Result: The quick brown fox jumps over something else.
Input text:
Command: grep myFunc
Output:
Command: grep myFunc -o
Output:
Command: grep -E myFunc.* -o
Output:
Discussion: Note the greedy matching of myFunc3 + myFunc4, this is discussed further below.
Command: sed -n "s|.*myFunc1('\(.*\)');.*|\1|p"
Output:
Discussion: The -n
option supresses / silences sed's default output of every line. The p
flag in the regex prints just the text which was substituted. The \1
substitution specifies the first matching group from the pattern. The group is the text contained within the set of escaped parentheses \(
& \)
. So this regex matches the entirety of any line which contains myFunc('ā¦')
, and prints out just the matching group value.
Command: sed -n "s|.*\(myFunc[0-9]\)('\(.*\)');.*|\1 === \2|p"
Output:
Discussion: Builds on previous example by matching any myFunc[N]
, and including that first match in a group. The output is changed to \1 === \2
to print both groups.
Note on greedy matching: POSIX regex doesn't support lazy / non-greedy captures (.*?
), which is why myFunc3
is omitted (the .*
captures it as part of the match). See below.
Command: grep -oE "myFunc[0-9]\('.*?'\);"
Output:
Discussion: We make use of a lazy / non-greedy capture here (.*?
, instead of .*
) with grep
to match against myFunc3
and myFunc4
separately. The -o
flag prints 'only' the match, and the -E
specifies we're using an expression. Note that unlike sed
, the parentheses must be escaped when we're attempting to match them explicitly (as opposed to escaping them to avoid matching them.)
Command: grep -oE "myFunc[0-9]\('.*?'\);" | sed -n "s|\(myFunc[0-9]\)('\(.*\)');|\1 === \2|p"
Output:
Discussion: Contrived example, fixes the greedy matching in sed
by first matching with the non-greedy grep
example further up. This gives us a exhaustive match across each line.
Command: sed "s|myFunc|theirFunc|"
Output:
Command: sed "s|myFunc|theirFunc|g"
Output:
Available via $1
, $2
, etc.
Available via $#
Available via $0
Available via $?
Backticks:
or $():
Piping one value to multiple commands
echo "Hi"| tee >(xargs echo) >(xargs echo) | xargs echo
Use
Useful for progress bars or printing multiple outputs on the same line during the script's running process. The example code below which leverages tput
avoids the leftover characters of the previously printed line.
echo "silence" &> /dev/null
echo 'l(100)/l(10)' | bc -l
To pretty-format JSON, pipe it into python
using json.tool. Example:
cat myJSONFile.json | python -m json.tool
curl -w "Connect time: %{time_connect} Time to first byte: %{time_starttransfer} Total: %{time_total} \n" "http://example.com/1/endpoint" -s -o /dev/null
curl "http://somewebsite.com/files[0001-0010].txt" -o "file_#1.txt"
zip -er myArchive.zip FolderName
The -r
flag provides recursion (zipping a folder). The password for the encryption by default will be entered at a prompt after the command is run.
unzip myArchive.zip
Sample script snippet. Also demonstrates use of terminal colors etc.
This repo contains a number of useful git commands and utilitiy scripts:
List of comparison operators:
Note: This trick is not compatible with some shells / environments. See also: .
curl
is the go-to utility for testing network endpoints, server responses, crafting forms, submitting POST requests, downloading files, and more: |