pushd

From RaySoft

Adds a directory to the top of the directory stack, or rotates the stack, making the new top of the stack the current working directory. With no arguments, exchanges the top two directories.[1]

Documentation

Syntax

pushd [DIRECTORY]

Examples

Redefine cd to use pushd and popd
my::_cd() {
  # Provide a 'cd' command replacement using 'pushd'.
  #
  # Arguments:
  #   $1: Path (optional)
  #
  # Returns:
  #   0:  Success
  #   >0: Error

  if [[ "$#" -eq 0 ]]; then
    set -- "${HOME}"
  fi

  if [[ ! -d "$1" ]]; then
    echo "Error finding directory: $1!"
    return 1
  fi

  if ! pushd "$1" >'/dev/null' 2>&1; then
    echo "Error changing directory: $1!"
    return 1
  fi

  return 0
}
alias -- -='popd >"/dev/null" 2>&1'

if declare -F my::_cd >'/dev/null' 2>&1; then
  alias cd='my::_cd'
fi

References

  1. bash -c 'help pushd'