LinuxCommandLibrary

pop

Remove and retrieve the last stack element

TLDR

Launch the Text-based User Interface

$ pop
copy

Send an email using the content of a Markdown file as body
$ pop < [path/to/message.md] --from [me@example.com] --to [you@example.com] --subject "[On the Subject of Ducks...]" --attach [path/to/attachment]
copy

Display help
$ pop --help
copy

SYNOPSIS

popd [+/-N]

PARAMETERS

+N
    Removes the Nth directory from the left side of the stack (0-indexed, where 0 is the top). The current directory becomes the Nth directory.

-N
    Removes the Nth directory from the right side of the stack (0-indexed, where 0 is the bottom). The current directory becomes the Nth directory.

(no argument)
    Removes the top directory from the stack and changes to the new top directory.

DESCRIPTION

The popd command removes a directory from the top of the shell's directory stack. This stack is managed in conjunction with the pushd command, which adds directories. When invoked without arguments, popd removes the current top directory and changes the working directory to the new top of the stack. If a numerical argument like +N or -N is provided, it removes the Nth directory from the stack (counting from the left or right, respectively) and makes it the current directory. This functionality is invaluable for efficient navigation between frequently accessed project directories or locations, allowing users to quickly jump back and forth without typing long paths repeatedly. It is a powerful tool for maintaining efficient context switching within the command line.

CAVEATS

popd is a shell built-in command, not an external executable. Its exact behavior might have minor differences between various shells (e.g., Bash, Zsh), but the core functionality remains consistent.
The directory stack is specific to the current shell session and is not preserved across different sessions.
Attempting to use popd on an empty stack or specifying a non-existent index will result in an error and a non-zero exit status.

<I>DIRECTORY STACK MANAGEMENT</I>

popd works in conjunction with pushd to manage the shell's directory stack, which is essentially a list of remembered directories. The dirs command can be used at any time to view the current contents and order of this stack. This powerful combination allows users to efficiently jump between frequently used directories and manage their navigation history seamlessly.

<I>RETURN STATUS</I>

The popd command typically returns a zero (success) status upon successful execution. A non-zero status is returned if the directory stack is empty when popd is called without arguments, or if a non-existent entry is specified by the +N or -N arguments, indicating an error.

HISTORY

The concept of a directory stack, along with the pushd and popd commands, first emerged in the C shell (csh) as a sophisticated method for managing directory navigation. Due to its significant utility for command-line users, this feature was subsequently adopted by other prominent shells like Bash and Zsh, becoming a standard and integral part of many POSIX-compliant shell environments. It offers a more dynamic and efficient alternative to traditional directory aliases or environment variables for frequently visited locations.

SEE ALSO

pushd(1), dirs(1)

Copied to clipboard