我发现cd -可以在之前和当前目录之间来回切换,这是一个非常方便的function。 也发现cd --也可以,但不知道究竟是什么。 有没有参考涵盖这个? 请指教,谢谢。
编辑:
我不清楚你是在说cd --还是cd -- - 。 所以,我会试着回答这两个问题。
cd -- (没有目录)将行为相同的cd (没有目录)。 您将被返回到您的主目录。 cd --不会切换回到以前的工作目录。
stefanl@host:~ $ cd tmp stefanl@host:~/tmp $ cd -- stefanl@host:~ $ cd -- # Note that I do not go back to the directory ~/tmp stefanl@host:~ $
看看这种行为是如何不同于cd - 。 对于cd -- -也是如此:
stefanl@host:~ $ cd tmp stefanl@host:~/tmp $ cd - /Users/stefanl stefanl@host:~ $ cd - /Users/stefanl/tmp # `cd -- -` behaves the same as `cd -` stefanl@host:~/tmp $ cd -- - /Users/stefanl stefanl@@host:~ $ cd -- - /Users/stefanl/tmp stefanl@host:~/tmp $
关于--对于大多数Unix命令:
--将告诉cd忽略cd后的任何选项。 从Bash Man页面 :
除非另有说明,否则每个内置命令logging为接受以“ – ”开头的选项,接受“ – ”表示选项结束。
假装你有一个名为-h的目录(注意我也需要在这里使用):
$ ls -ld -- -h drwxr-xr-x 2 stefanl stefanl 68 Nov 8 16:41 -h
你不能正常的cd,因为这个命令试图把-h解释为一个选项:
$ cd -h -bash: cd: -h: invalid option cd: usage: cd [-L|-P] [dir]
所以使用--告诉CD不要在--之后处理更多的选项:
stefanl@host:~ $ cd -- -h stefanl@host:~/-h $ pwd /Users/stefanl/-h stefanl@host:~/-h $
以前的目录也是cd ~-
取决于你的shell,但看看内置的dirs (在这种情况下,我的是zsh ):
[iluvatar]-[/srv/django]-[1126] % dirs -v 0 /srv/django 1 /srv/django/app 2 /srv/django/app2 3 ~
然后,您可以使用cd ~N快捷键来更改目录,以及push和popd来更改堆栈。
我当然不知道这个答案,但是根据我的推论,这似乎被解释为争论的先兆。 由于没有parameter passing,它被忽略,所以默认为cd ,它会将您带到您的主目录。
这似乎发生了几个标准的使用情况:
[james@aladdin blah]$ pwd
/home/james/blah
[james@aladdin blah]$ touch hello
[james@aladdin blah]$ ls -
ls: cannot access -: No such file or directory
[james@aladdin blah]$ ls --
hello
[james@aladdin blah]$ rm -
rm: cannot remove '-': No such file or directory
[james@aladdin blah]$ rm --
rm: missing operand
Try 'rm --help' for more information.