cd到名为`-`的目录


2

我正在尝试创建一个名为的文件夹 -。我知道这是一个愚蠢的名字,它被命名为反映别的东西。

mkdir -
ls
> -
cd -

这使它上升到一个文件夹或退出符号链接上下文取决于我在哪里。我找到 cd - 手段 转到以前的位置 ,但无法摆脱它。 cd \- 不起作用。

有什么方法吗?

Answers:


9

您可以使用相对路径或绝对路径来防止 - 单独站立并混淆bash的构建命令 cd

mkdir ./-
cd ./-

尼斯。我发现在发布之后我可以上升到一个水平并向下导航两个级别,但你的答案更好。
SimplGy

1

对面 ~,由bash评估, - 是一个论据 cd。看看手册页(好吧,对于当前系统都在 man bashcd 是内置的):

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

关于波浪扩展的段落(简化为最常见的含义):

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.

~ 在将参数传递给实际命令之前,将被bash替换。因此,以某种方式逃避它(例如使用 '~' 要么 \~ ) 将工作。 - 另一方面将传递给命令。转义没有用处:bash无论如何都不会对它进行评估,但它将作为单个字符串传递给未转义 cd,随后将开放 $OLDPWD


Patrix更合理的开放方式,是一种更为深奥的替代品 - 文件夹将设置 $OLDPATH-

OLDPATH=- cd -

我喜欢深奥的选择:-)
nohillside
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.