Fix HEAD ref lookups, and compatibility for non-bash shells.