linux – 如何使用此名称“-2”(以连字符开头)进入cd目录?

前端之家收集整理的这篇文章主要介绍了linux – 如何使用此名称“-2”(以连字符开头)进入cd目录?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个名为的目录:
-2

我想要进入它,但CD抱怨:

bash: cd: -2: invalid option

没有成功,我试过:

cd "-2"
cd '-2'
cd \-2

解决方案吗

编辑:服务器上没有像mc等文件浏览器.

解决方法

至少有两种方式:

>使用 – 参数.

cd -- -2

这使用GNU工具常用的约定,它不会处理之后出现的任何内容 – 作为命令行选项.

正如commenter所述,该公约也是defined in the POSIX standard

Default Behavior: When this section is listed as “None.”,it means that the implementation need not support any options. Standard utilities that do not accept options,but that do accept operands,shall recognize "--" as a first argument to be discarded.

The requirement for recognizing "--" is because conforming applications need a way to shield their operands from any arbitrary options that the implementation may provide as an extension. For example,if the standard utility foo is listed as taking no options,and the application needed to give it a pathname with a leading hyphen,it could safely do it as:

06001

and avoid any problems with -m used as an extension.

as well as

Guideline 10:
The argument -- should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands,even if they begin with the '-' character. The -- argument should not be used as an option or as an operand.

>明确指定路径:

cd ./-2

这指定了显式命名当前目录(.)作为起点的路径.

cd $(pwd)/-2
cd /absolute/path/to/-2

这些是上面的变化.任何数量的此类变化都是可能的;我将把它作为练习留给读者发现所有这些.

猜你在找的Linux相关文章