如何在一个命令行中使用多个双破折号( - )

时间:2016-03-12 04:34:00

标签: bash

例如

ssh root@me -- cat /etc/hostname -- cat /etc/hostname

我希望它输出:

me
me

但输出

me
me
cat: cat: No such file or directory

我知道双破折号意味着结束解析选项,为什么它会引发cat: cat: No such file or directory

1 个答案:

答案 0 :(得分:9)

--表示选项解析结束。 --之后的任何内容都不会被视为一个选项,即使它以短划线开头。例如,ls -l将以长格式打印文件列表,而ls -- -l会查找名为-l的文件。

ssh root@me -- cat /etc/hostname -- cat /etc/hostname

此sshes到远程服务器并运行命令:

cat /etc/hostname -- cat /etc/hostname

这是一个单一的猫命令。跳过--,它等同于写作:

cat /etc/hostname cat /etc/hostname

打印/etc/hostname,即me。然后,它会尝试打印不存在的文件cat,并提供错误cat: cat: No such file or directory。程序猫抱怨文件cat不存在。然后再次打印/etc/hostname

如果要使用ssh执行多个命令,请执行以下操作:

ssh root@me 'cat /etc/hostname; cat /etc/hostname'

或者这个:

ssh root@me <<CMDS
cat /etc/hostname
cat /etc/hostname
CMDS