Recently I find I can't use cd - to go back to last directory sometimes. It gives the error of -bash: cd: ~/whatever_dir: No such file or directory. A closer look shows that the error only happens if the last directory contains tilde. It seems tilde cannot be expanded. "cd ~", however, can successfully change to my home directory. What's the problem? I know tilde won't expand inside quotes, but the last directory stored is not in qotes when executing cd -, right? Plus, cd - worked for me previously.

link|improve this question
What is the value of $OLDPWD when this phenomenon appears? – fge Dec 16 '11 at 22:08
@fge: As seen by typing echo "$OLDPWD", not echo $OLDPWD – Keith Thompson Dec 16 '11 at 22:14
@KeithThompson that doesn't matter for echo – fge Dec 16 '11 at 22:27
@fge: It looks like you're right (but the quotation marks won't hurt, and remove some amount of uncertainty). – Keith Thompson Dec 16 '11 at 22:43
Both of the echo commands show ~/whatever_dir correctly. – RNAer Dec 17 '11 at 3:24
feedback

migrated from stackoverflow.com Dec 17 '11 at 11:33

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 0 down vote accepted

The likely source of the problem, executed as part of PROMPT_COMMAND in your bash configuration:

PWD="${PWD/$HOME/~}"

Stop reassigning PWD to a different value. It will just break things.

link|improve this answer
Indeed. Thanks. – RNAer Dec 19 '11 at 21:20
feedback

You have a serious bug in your shell which you should report. You said that in such a situation, $OLDPWD shows ~/whatever_dir. It should not.

Here is what I have here:

fge@erwin ~/whatever_dir $ pwd
/home/fge/whatever_dir
fge@erwin ~/whatever_dir $ cd -
/home/fge
fge@erwin ~ $ echo $OLDPWD
/home/fge/whatever_dir

~ is just a convenient shortcut used by shells to signify your home directory. Mine is /home/fge. In any event, $OLDPWD should show an absolute path, NOT a path with ~ in it, except if you do have a directory named ~.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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