Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

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

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

    
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

2 Answers 2

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.

share|improve this answer
    
Indeed. Thanks. –  RNA Dec 19 '11 at 21:20

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 ~.

share|improve this answer

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.