Could anybody explain to me how cd - command work? man cd tells me that An argument of - is equivalent to $OLDPWD. Then I found on the net that $OLDPWD is the previous working directory as set by the cd command. And when I do cd - on my unix I get -bash: cd: OLDPWD not set which is confusing for me.

UPDATE: it works now it gives me the previous working directory. I guess I did not use and cd command before so that is why $OLDPWD was not set.

One more question what cd (type cd and press enter) does? I wouldn't say that nothing. Is there any better technical explanation?

host [~]# cd
host [~]#

closed as off topic by Michael Hampton, John Gardeniers, mdpc, 87cd25770a, Ward Feb 7 '13 at 4:25

Questions on Server Fault are expected to relate to server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question.


OLDPWD is not set, because you have't changed directory

[dave@odessa ~]$ cd -
-bash: cd: OLDPWD not set
[dave@odessa ~]$ cd /tmp
[dave@odessa tmp]$ cd -

[dave@odessa ~]$ cd /tmp
[dave@odessa tmp]$ echo $OLDPWD

cd without any arguments will chdir to $HOME

[dave@odessa tmp]$ echo $HOME
[dave@odessa tmp]$ HOME=/ cd
[dave@odessa /]$ pwd
  • 1
    @Dave Cheney: thank you Dave, great answer. Crystal clear now ... – Radek Mar 3 '10 at 10:10
type cd

tells us that

cd is a shell builtin

man sh

tells us what you found out:

If a single dash is specified as the argument, it will be replaced by the value of OLDPWD.

The internal implementation of cd in the shell does a chdir(2) -syscall.

  • @ptman: looks like we are having different results with different shells in both cd - and cd cases – Radek Mar 3 '10 at 9:41
  • 1
    Nice, I never knew about type. I was used to man (some shell builtin) giving me the raspberry. – Dave Cheney Mar 3 '10 at 9:54

cd by itself changes to your home directory

$ cd /tmp
$ pwd
$ cd
$ pwd
  • @Dennis Williamson: yes, yes, I already forgot ... – Radek Mar 3 '10 at 10:10

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