How can I set the current working directory to the directory of the script in Bash?

#!/bin/bash
cd "$(dirname "$0")"

The following also works:

cd "${0%/*}"

The syntax is thoroughly described in this StackOverflow answer.