Tim*_*Tim 45 options cd-command arguments
cd - 可以在当前目录和上一个目录之间切换。
似乎我以前见过-用作其他命令的参数,但我不记得 if-与cd.
我发现这-不适用于ls.
是- 只用cd吗?
cuo*_*glm 46
-在POSIX 实用程序语法指南中定义为标准输入:
Guideline 13:
For utilities that use operands to represent files to be opened for either
reading or writing, the '-' operand should be used to mean only standard input
(or standard output when it is clear from context that an output file is being
specified) or a file named -.
Run Code Online (Sandbox Code Playgroud)
您可以看到对文件进行读取或写入操作的实用程序的定义。cd不属于这些实用程序,因此-在 cd 中不遵循此准则。
此外,POSIX 还定义-了cd有自己的含义:
-
When a <hyphen> is used as the operand, this shall be equivalent to the
command:
cd "$OLDPWD" && pwd
which changes to the previous working directory and then writes its name.
Run Code Online (Sandbox Code Playgroud)
Law*_*nce 36
cd -实际上是 的简写cd "$OLDPWD" && pwd,$OLDPWD每次将目录更改为刚刚所在的目录时都会设置where 。
处理-取决于应用程序。一些应用程序-用来表示STDIN,例如grep,awk
其他的应用可以-为任何一个速记他们选择,因为迈克尔的回答指定,有su,-是简写--login
Gil*_*il' 22
的POSIX实用程序语法准则(具体#13)指定为期望的文件名,从读公用事业-装置标准输入,以及用于该期望的文件名写入到,公用事业-装置标准输出。例如,cat somefile -将 的内容复制somefile到其标准输出,然后是它在其标准输入上读取的内容。
此准则不适用于该cd命令,因为它不读取或写入文件。cd做了一些不同的事情:参数的-意思是“上一个目录”。该命令cd -等效于cd "$OLDPWD" && pwd. 此行为特定于cd命令,以及直接启发的命令,例如pushd.
请注意,这-是一个操作数,而不是一个选项。只有以-and开头的参数不只是-或是--选项。作为操作数的主要含义是--不影响其特殊含义。例如,cd -- -P更改为名为 的子目录-P,但cd -- -与 相同cd -,它不会更改为名为 的目录-。同样,cat -- -不从调用的文件中读取,-而是从标准输入中读取。
dot*_*hen 19
虽然迈克尔提到su和其他应用程序可以使用-意味着为所欲为(读取标准输入是常见的),GIT中确实使用-类似于如何一种时尚cd呢,变更分支机构。
$ git status
On branch master
$ git checkout foobar
$ git status
On branch foobar
$ git checkout -
$ git status
On branch master
Run Code Online (Sandbox Code Playgroud)