Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 1 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .