Hacker News new | ask | show | jobs
by dastx 2016 days ago
Really scripts should avoid called `cd` or `pushd`/`popd` wherever possible. It's a lot better to just work with absolute directories, i.e. as someone else pointed out:

    DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
1 comments

no no no... god this is gross and awful

  SELF=${0##*/}    # aka basename
  DIR=${0%/*}      # aka dirname
Running a script as sh script.sh returns only script.sh, thus, you've already got SELF, but you wouldn't get DIR.
but, you don't need it since you are already there, and can just use $PWD (no need for `pwd`) from that point onward.