Navigate to Script Directory


Often times when writing scripts I want to reference files in the same directory, but keep the script portable in case it is part of a git repository being checked out somewhere else or just the folder getting moved.

This little snippet gets me the scripts folder, from which I can then navigate to it or build other paths from there.

script_dir="$(cd -P -- "$(dirname -- "$0")" && pwd -P)"

# Navigate to it
cd $script_dir

# Build paths from it
echo "Starting..." >> $script_dir/run.log

See also