1

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?

Andrea
  • 1,536

1 Answers1

7

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.