Is there a command to copy the above line within terminal? (e.g. copy the output of pwd)

Is there a command to copy the above line in terminal? This is a shell agnostic question (e.g. bash, zsh, etc.) basically I am using pwd and want to copy the output without having to type the long directory within my next command.

1 Answer

Bash doesn't automatically store the stdout or stderr of the previous command. It mainly doesn't do this because of performance reasons. It could potentially take up a lot of RAM if the stdout output was very large, as is sometimes the case.

This is also why terminal emulators, everything from the built-in Linux VT up to gnome-terminal, impose a reasonably small limit on the amount of scrollback buffers (number of lines you can scroll up) before it "wraps".

Taking up that much RAM automatically and without user prompting would greatly slow down the shell for the many scripts and command sequences that don't require this particular behavior.

Instead, the user must know in advance that they want to store the stdout, and can then reference it in their later commands.

Two ways of doing this:

  • echo $(pwd) -- in-line
  • MYPWD=$(pwd); echo ${MYPWD} -- separate variable to capture output then reference it later

BTW, you might check the PWD environment variable. It may help you use way 2.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like