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

port=${1:-8888}
echo $port

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