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

mkdir -
> -
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?


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

mkdir ./-
cd ./-
  • Nice. I realized after I posted I could go up a level and navigate back down two levels but your answer is better. – SimplGy Feb 19 '14 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 -
  • I like the esoteric alternative :-) – nohillside Feb 20 '14 at 19:23

You must log in to answer this question.

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