您的位置:首页 > 运维架构 > Shell

bash中的特殊文件夹

2016-03-15 12:20 459 查看
今天整理磁盘的时候发现一个文件夹叫做‘-help'

$ ls -F
-help/

尝试进去看看里边有什么东西,却发现怎么也进不去

$ cd -help
bash: cd: -h: invalid option
cd: usage: cd [-L|-P] [dir]
$ cd '-help'
bash: cd: -h: invalid option
cd: usage: cd [-L|-P] [dir]
$ cd "\-help"
bash: cd: \-help: No such file or directory

对于其他的特殊字符,通常单引号就可以搞定了

$ ls -F
help/  #help/  -help/ \help/
$ cd #help
$ pwd
~
$ cd -
cd /tmp
$ cd '#help'
$ pwd
/tmp/#help
$ cd ..
$ cd ' help'
$ pwd
/tmp/ help
$ cd ..
$ cd '\help'
$ pwd
/tmp/\help

那'-help'到底要怎么进呢。 在这里不得不再次吐槽一下百度,怎样都查不到想要的结果,“cd 减号开头 目录” “cd -开头目录” “cd 特殊字符目录” 全部查不到! 查!不!到!

只好google,一查必中,都不用全部输入,输入'cd -' 相关推荐就是我想要的东西了。

下边是正确答案了,如果你看我啰嗦到这里,我相信,是真爱,哈哈哈。

答案来自 ServerFault,两种方法,其中“--” 这个选项,又是我所不知道的,要学的东西太多,加油!

Use the
--
argument.
cd -- -2
This uses a convention common to GNU tools which is to not treat anything that appears after
--
as a command line option.
As a commenter noted, this convention is also 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:

foo -- -myfile

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.

Specify the path explicitly:
cd ./-2
This specifies the path explicitly naming the current directory (
.
) as the starting point.
cd $(pwd)/-2
cd /absolute/path/to/-2
These are variations on the above. Any number of such variations may be possible; I'll leave it as an exercise to the reader to discover all of them.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: