Indenting bash

Published: Tuesday, Aug 7, 2012 Last modified: Monday, Apr 8, 2024

Say you have a function like so:

wait_for()
{
tmp=$(mktemp -u)
mkfifo $tmp
inotifywait -m --format=%f -e create "${1%/*}" > $tmp 2>&1 &
while read output
do
case $output in "Watches established.") test -p "$1" && break ;; "${1##*/}") break ;; esac
done < $tmp
rm -f $tmp
}

If you run it with the command:

type wait_for

You will get re-formatted bash code like so:

wait_for is a function
wait_for ()
{
	tmp=$(mktemp -u);
	mkfifo $tmp;
	inotifywait -m --format=%f -e create "${1%/*}" > $tmp 2
>&1 & while read output; do
		case $output in
			"Watches established.")
				test -p "$1" && break
			;;
			"${1##*/}")
				break
			;;
		esac;
	done < $tmp;
	rm -f $tmp
}

Lovely eh?