Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 233523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:06:34+00:00 2026-05-11T20:06:34+00:00

Is there an equivalent of the bash pushd/popd build in commands for the KSH?

  • 0

Is there an equivalent of the bash pushd/popd build in commands for the KSH?

For those who don’t know what pushd and popd in bash do, here is the description from the man page

   pushd [-n] [dir]
   pushd [-n] [+n] [-n]
          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 directo-
          ries and returns 0, unless the directory stack is empty.


   popd [-n] [+n] [-n]
          Removes entries from the directory stack.  With  no  arguments,
          removes  the top directory from the stack, and performs a cd to
          the new top directory.  Arguments, if supplied, have  the  fol-
          lowing meanings:

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:06:35+00:00Added an answer on May 11, 2026 at 8:06 pm

    When I discovered that ksh didn’t include these, I wrote my own. I put this in ~/bin/dirstack.ksh and my .kshrc file includes it like this:

    . ~/bin/dirstack.ksh
    

    Here are the contents of dirstack.ksh:

    # Implement a csh-like directory stack in ksh
    #
    # environment variable dir_stack contains all directory entries except
    # the current directory
    
    unset dir_stack
    export dir_stack
    
    
    # Three forms of the pushd command:
    #    pushd        - swap the top two stack entries
    #    pushd +3     - swap top stack entry and entry 3 from top
    #    pushd newdir - cd to newdir, creating new stack entry
    
    function pushd
    {
       sd=${#dir_stack[*]}  # get total stack depth
       if [ $1 ] ; then
          if [ ${1#\+[0-9]*} ] ; then
             # ======= "pushd dir" =======
    
             # is "dir" reachable?
             if [ `(cd $1) 2>/dev/null; echo $?` -ne 0 ] ; then
                cd $1               # get the actual shell error message
                return 1            # return complaint status
             fi
    
             # yes, we can reach the new directory; continue
    
             (( sd = sd + 1 ))      # stack gets one deeper
             dir_stack[sd]=$PWD
             cd $1
             # check for duplicate stack entries
             # current "top of stack" = ids; compare ids+dsdel to $PWD
             # either "ids" or "dsdel" must increment with each loop
             #
             (( ids = 1 ))          # loop from bottom of stack up
             (( dsdel = 0 ))        # no deleted entries yet
             while [ ids+dsdel -le sd ] ; do
                if [ "${dir_stack[ids+dsdel]}" = "$PWD" ] ; then
                   (( dsdel = dsdel + 1 ))  # logically remove duplicate
                else
                   if [ dsdel -gt 0 ] ; then        # copy down
                      dir_stack[ids]="${dir_stack[ids+dsdel]}"
                   fi
                   (( ids = ids + 1 ))
                fi
             done
    
             # delete any junk left at stack top (after deleting dups)
    
             while [ ids -le sd ] ; do
                unset dir_stack[ids]
                (( ids = ids + 1 ))
             done
             unset ids
             unset dsdel
          else
             # ======= "pushd +n" =======
             (( sd = sd + 1 - ${1#\+} ))    # Go 'n - 1' down from the stack top
             if [ sd -lt 1 ] ; then (( sd = 1 )) ; fi
             cd ${dir_stack[sd]}            # Swap stack top with +n position
             dir_stack[sd]=$OLDPWD
          fi
       else
          #    ======= "pushd" =======
          cd ${dir_stack[sd]}       # Swap stack top with +1 position
          dir_stack[sd]=$OLDPWD
       fi
    }
    
    function popd
    {
       sd=${#dir_stack[*]}
       if [ $sd -gt 0 ] ; then
          cd ${dir_stack[sd]}
          unset dir_stack[sd]
       else
          cd ~
       fi
    }
    
    function dirs
    {
       echo "0: $PWD"
       sd=${#dir_stack[*]}
       (( ind = 1 ))
       while [ $sd -gt 0 ]
       do
          echo "$ind: ${dir_stack[sd]}"
          (( sd = sd - 1 ))
          (( ind = ind + 1 ))
       done
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a Bash equivalent to the Python's pass statement?
Are there equivalent to perl -c syntax check for JavaScript from command? Given that
Is there an equivalent in Java 1.5 to Enumerable.Cast() from C#? I want to
Is there an equivalent to 1/countif in SQL Server? Something like: SELECT ID,1/COUNTIF(ID) FROM
I know how to configure aliases in bash, but is there a way to
I frequently execute from a shell (in my case Bash) commands that I want
Is there a simple Windows equivalent for this bash command: head -c 500 /dev/urandom
For example, is there equivalent of these in SQL*Plus sqlplus 'SELECT * FROM emp'
Is there equivalent to \Q ... \E in C# Regex? I can't find it.
Is there an equivalent Matlab dot function in numpy? The dot function in Matlab:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.