Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I'm trying to make a folder called -. I know it's a silly name, it's named so to mirror something else.

mkdir -
ls
> -
cd -

This makes it go up a folder or exit the symlink context depending on where I am. I found that cd - means go to previous location, but can't escape it. cd \- does not work.

Any way around this?

share|improve this question

2 Answers 2

up vote 9 down vote accepted

You can use either relative or absolute paths to prevent the - from standing alone and confusing bash's build in command cd:

mkdir ./-
cd ./-
share|improve this answer
    
Nice. I realized after I posted I could go up a level and navigate back down two levels but your answer is better. –  Simple As Could Be Feb 19 at 21:23

Opposite to ~, which gets evaluated by bash, - is an argument to cd. Have a look at the man pages (well, for current systems both are in man bash as cd is a builtin):

   cd [-L|[-P [-e]]] [dir]
          Change the current directory to dir. [...]  An argument of - is equivalent
          to $OLDPWD. [...]

And the paragraph on tilde expansion (simplified to the most common meaning):

Tilde Expansion
   If  a  word begins with an unquoted tilde character (`~'), [...] the tilde-prefix 
   is replaced with the home  directory associated with the specified login name.

~ will be replaced by bash before passing the argument to the actual command. Thus, escaping it somehow (for example using '~' or \~) will work. - on the other hand will be passed to the command. Escaping has no use: bash will not evaluate it anyway, but it will get passed unescaped as single string to cd, which will subsequently open $OLDPWD.


A more esoteric alternative to Patrix' more reasonable way to open --folders would be to set $OLDPATH to -:

OLDPATH=- cd -
share|improve this answer
    
I like the esoteric alternative :-) –  patrix Feb 20 at 19:23

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.