cd to directory with name of `-`
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?
You can use either relative or absolute paths to prevent the - from standing alone and confusing bash's build in command cd
:
mkdir ./-
cd ./-
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 -