cd-(cd连字符)有什么作用?


14

cd -今天错误地输入了终端,但出现错误消息:

bash: cd: OLDPWD not set

不幸的是,man cd不存在。

No manual entry for cd

它实际上是做什么的?


3
cd是Bash内置的。因此man bash-builtins,该文档记录在中,而不是其自己的页面中。这适用于所有其他Bash内置程序。由于bash: 错误消息的前面,您可以说它是内置的。
kiri 2014年

Answers:


17

cd - 在新旧工作目录之间切换。

avinash @ avinash-Lenovo-IdeaPad-Z500:〜$ cd-
bash:cd:未设置OLDPWD
avinash @ avinash-Lenovo-IdeaPad-Z500:〜$ cd〜/台式机
avinash @ avinash-Lenovo-IdeaPad-Z500:〜/ Desktop $ pwd
/ home / avinash / Desktop
avinash @ avinash-Lenovo-IdeaPad-Z500:〜/ Desktop $ cd-
/ home / avinash
avinash @ avinash-Lenovo-IdeaPad-Z500:〜$ 

也可以看看,

avinash @ avinash-Lenovo-IdeaPad-Z500:〜$ echo $ OLDPWD

avinash @ avinash-Lenovo-IdeaPad-Z500:〜$ cd〜/台式机
avinash @ avinash-Lenovo-IdeaPad-Z500:〜/ Desktop $ echo $ OLDPWD
/ home / avinash
avinash @ avinash-Lenovo-IdeaPad-Z500:〜/ Desktop $ cd d
avinash @ avinash-Lenovo-IdeaPad-Z500:〜/ Desktop / d $ echo $ OLDPWD
/ home / avinash / Desktop
avinash @ avinash-Lenovo-IdeaPad-Z500:〜/ Desktop / d $ 

$OLDPWD变量存储先前的当前工作目录的路径。


8

Avinash Raj的答案是完全正确的,但对于手册条目,您可以获取POSIX手册页,然后man cd可以使用:

sudo apt-get install manpages-posix
man cd

告诉您这一切的是OPERANDS部分:

-      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.
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.