1

I've been using pushd and popd for a long time while writing bash script. But today when I execute which pushd, I get nothing as output. I can't understand this at all. I was always thinking that pushd is simply a command, just like cd, ls etc.

So why does which pushd give me nothing?

3

popd and pushd are commands built into Bash, they're not actual executables that live on your HDD as true binaries.

excerpt bash man page
   DIRSTACK
          An array variable (see Arrays below) containing the current 
          contents of the directory stack.  Directories appear in the stack 
          in the order they are displayed by the dirs builtin.  Assigning to 
          members of  this  array variable may be used to modify directories 
          already in the stack, but the pushd and popd builtins must be used 
          to add and remove directories.  Assignment to this variable will 
          not change the current directory.  If DIRSTACK is unset, it loses 
          its special properties, even if it is subsequently reset.

The full list of all the builtin commands is available in the Bash man page as well as here - http://structure.usc.edu/bash/bashref_4.html.

You can also use compgen -b or enable to get a full list of all these builtins:

compgen
$ compgen -b | grep -E "^push|^pop"
popd
pushd
enable
$ enable -a | grep -E "\bpop|\bpus"
enable popd
enable pushd

Additionally if you want to get help on the builtins you can use the help command:

$ help popd | head -5
popd: popd [-n] [+N | -N]
    Remove directories from stack.

    Removes entries from the directory stack.  With no arguments, removes
    the top directory from the stack, and changes to the new top directory.

$ help pushd | head -5
pushd: pushd [-n] [+N | -N | dir]
    Add directories to stack.

    Adds a directory to the top of the directory stack, or rotates
    the stack, making the new top of the stack the current working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.