Tips for Bash Scripts

Bash Shell Scripting is powerful way of automating shell commands. But its syntax is archaic so it’s hard to remember everything. In this blog post, I try to keep track of all the stuff that I learned. It will get updated occasionally.

1. The Start

#!/usr/bin/env bash
set -e
set -x

Beginn every script with those 3 lines. The first make sure it runs on as many devices as possible (see SO), the second lines aborts the whole script if one command failds, the third line prints the command that gets executed.

2. Default Parameters

echo $port

This takes the first positional paramters as port or 8888 as fallback.

3. Run a command N times

for run in {1..10}
for run in {1..10}; do command; done


4. Run a command indefinitely

while true; do command; done

More material: 1