Shebang and Running Scripts
The shebang line
The first line tells the kernel which interpreter to use. env finds bash on your PATH.
#!/usr/bin/env bash
# hello.sh
echo "Hello"
Make it executable
Create the file, then chmod and run with a relative path.
chmod +x hello.sh
./hello.sh
Or pass to bash
Useful while testing: bash hello.sh does not require the executable bit.
bash hello.sh
bash -n hello.sh # syntax check only
Working directory
Scripts start in the caller's current directory unless you change it. Prefer locating files relative to the script when needed.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo "$SCRIPT_DIR"
Comments
One comment per signed-in account. Comments are saved with this page’s URL.