Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I typed cd - in terminal by mistake today, and I got an error saying:

bash: cd: OLDPWD not set

And unfortunately, man cd doesn't exist.

No manual entry for cd

What does it actually do?

share|improve this question
1  
cd is a Bash builtin. It is therefore documented in man bash-builtins, instead of its own page. This holds true for all other Bash builtins. You can tell it's a builtin because of the bash: in front of the error message. –  minerz029 Mar 15 at 11:26

2 Answers 2

up vote 4 down vote accepted

cd - switches between the old and new present working directories.

avinash@avinash-Lenovo-IdeaPad-Z500:~$ cd -
bash: cd: OLDPWD not set
avinash@avinash-Lenovo-IdeaPad-Z500:~$ cd ~/Desktop
avinash@avinash-Lenovo-IdeaPad-Z500:~/Desktop$ pwd
/home/avinash/Desktop
avinash@avinash-Lenovo-IdeaPad-Z500:~/Desktop$ cd -
/home/avinash
avinash@avinash-Lenovo-IdeaPad-Z500:~$ 

See also,

avinash@avinash-Lenovo-IdeaPad-Z500:~$ echo $OLDPWD

avinash@avinash-Lenovo-IdeaPad-Z500:~$ cd ~/Desktop
avinash@avinash-Lenovo-IdeaPad-Z500:~/Desktop$ echo $OLDPWD
/home/avinash
avinash@avinash-Lenovo-IdeaPad-Z500:~/Desktop$ cd d
avinash@avinash-Lenovo-IdeaPad-Z500:~/Desktop/d$ echo $OLDPWD
/home/avinash/Desktop
avinash@avinash-Lenovo-IdeaPad-Z500:~/Desktop/d$ 

The $OLDPWD variable stores the path of previous present working directory.

share|improve this answer

Avinash Raj's answer is completely correct but as for the manual entry, you can get the POSIX manual pages and then man cd will work:

sudo apt-get install manpages-posix
man cd

The bit that tells you all this is the OPERANDS section:

-      When a hyphen is used as the operand, this shall be equivalent to the command:

       cd "$OLDPWD" && pwd

which changes to the previous working directory and then writes its name.
share|improve this answer
    
Cool, didn't know they weren't installed by default. Should have searched the online Ubuntu manpages. –  Dan Jan 22 at 12:06

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.