# @ change directory, storing new directory on DIRSTACK
cd() {
  ## variables for directory and return code
  # local dir error

  ## ignore all options
  while :; do
    case $1 in
      --) break ;;
      -*) shift ;;
      *) break ;;
    esac
  done

  dir=$1

  ## if a $dir is not empty
  if [ -n "${dir}" ]
  then
    ## change directory
    # shellcheck disable=SC2164
    pushd "${dir}"
  else
    ## go HOME if nothing on the command line
    # shellcheck disable=SC2164
    pushd "${HOME}"
  fi 2>/dev/null             ## error message should come from cd, not pushd

  error=$?     ## store pushd's exit code

  if [ "${error}" -ne 0 ]    ## failed, print error message
  then
    # shellcheck disable=SC2164
    builtin cd "${dir}"      ## let the builtin cd provide the error message
  fi
  return "${error}"          ## leave with pushd's exit code
} > /dev/null
