Bash Patterns I Use Weekly

1. Find and replace a pattern in a codebase with capture groups

git grep -l pattern | xargs gsed -ri 's|pat(tern)|\1s are birds|g'

2. Track down a commit when a command started failing

while command; do git checkout HEAD^; done;

3. Parallelize running commands by grabbing PIDs.

do_thing_1 &
pids="$pids $!"
do_thing_2 &
pids="$pids $!"
for p in $pids; do
  if ! wait $p; then EXIT_CODE=1; fi

exit $EXIT_CODE;

4. Use $SECONDS to track how long things take

echo "Your command completed after $SECONDS seconds";

5. Use for to iterate over simple lists:

for route in foo bar baz; do
  curl localhost:8080/$route

The default separator in bash is a space, and you can take advantage of this to make simple for loops. (This default is quite frustrating to work around in general: spaces in file names are the devil.)