Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer

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  
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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