pushd and popd for bourne shell

abcscnuk at csun.UUCP abcscnuk at csun.UUCP
Fri Feb 6 09:02:33 AEST 1987


        Here are some shell functions to implement ``pushd'' and ``popd'' for
bourne shell.  They should behave much in the same way as the ``pushd'' and
``popd'' commands in csh.  Having gotten used to using them in csh, I couldn't
live without them, so I decided to write up shell functions that will behave
similarly.

        If you're unfamiliar with ``pushd'' and ``popd,'' here are brief
descriptions:

        pushd -- ``cd'' to a directory (if one is specified) and save the
		 previous PWD into a directory stack.   If no arguments are
		 given, it cd's you to the directory at the top of the
		 directory stack and puts present PWD at the top of the
		 stack  (Essentially switches between the present directory
		 and directory at top of directory stack).

	popd  -- pop the top of directory stack off and ``cd'' to it.

							Naoto Kimura
                //-n-\\					(csun!abcscnuk)
        _____---=======---_____
    ====____\   /.. ..\   /____====
  //         ---\__O__/---        \\	Enterprise... Surrender or we'll
  \_\                            /_/	send back your *&^$% tribbles !!!
----- cut here ----- cut here ----- cut here ----- cut here -----
DIRSTAK=""

dirs() {
   echo `pwd` $DIRSTAK
}

pushdir() {
   if test $# -eq 0
   then
      if test "$DIRSTAK" = ""
      then
         echo "directory stack empty."
      else
	 OLDPWD=`pwd`
         popdir
         DIRSTAK="$OLDPWD $DIRSTAK"
      fi
   else
      while test "$*" != ""
      do
	 OLDPWD=`pwd`
         if cd "$1"
         then
            DIRSTAK="$OLDPWD $DIRSTAK"
         fi
         shift
      done
   fi
}

listhead() {
   echo $1
}

listtail() {
   shift
   echo $*
}

popdir() {
   if test $# -ne 0
   then
      echo "No parameters allowed with popdir"
   else
      if test "$DIRSTAK" = ""
      then
         echo "directory stack empty."
      else
         TMP1=`listhead $DIRSTAK`
         DIRSTAK=`listtail $DIRSTAK`
         if test "$TMP1" != ""
         then
            cd "$TMP1"
         fi
      fi
   fi
}

pushd() {
   if pushdir $*
   then
      dirs
   fi
}

popd() {
   if popdir $*
   then
      dirs
   fi
}



More information about the Comp.sources.unix mailing list