Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For instance, let's say I wish to type the command pwd.

It gives me a really long path to the current directory I am in.

What I would really like to do is make it so that I can go into a completely different directory and still refer to the previous directory I was in, without having to memorize the previous path.

Is there any way to do this?

share|improve this question

You involve several different questions in one. :P
We'll cover variable assignment first.
To put the result of a command into a variable you can either use backticks `` or $() syntax. For example:

MYPWD=`pwd`
MYPWD=$(pwd)

For further information check the section Command Substitution in the bash(1) manual page. Easily available at: http://linux.die.net/man/1/bash

But if for your question is about changing to the previous directory bash has a built in syntax for this. Simply do a cd - and bash will take you to where you were.

# cd /usr/local/bin
# cd /var/log/apache2
# cd -
# pwd
/usr/local/bin

For more advanced directory/cd handling, check the manual page for pushd and popd that allows you to build a stack of directories that you can use.

share|improve this answer
1  
Maybe also mention that ${OLDPWD} is a variable that already contains the previous directory. – nicerobot Jan 7 '12 at 6:08
2  

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .