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?


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:


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

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.

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.