好好爱自己!

What's the difference between - (one hyphen) and — (two hyphens) in a command?

bash中看到这样的命令,

curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs

黄色部分,| 这个是管道操作符,表示前面命令的输出作为后面的命令的输入。 "bash -" bash 跟一个短杠的作用是什么呢?
For a command, if using - as an argument in place of a file name will mean STDIN or STDOUT.

参考:https://askubuntu.com/questions/813303/whats-the-difference-between-one-hyphen-and-two-hyphens-in-a-command

---------------------------------------------------------

Generally:

  • - means to read the argument/content from STDIN (file descriptor 0)

  • -- means end of command options, everything follows that are arguments

Why needed:

About -:

$ echo foobar | cat -
foobar

Although cat can read content from STDIN without needing the -, many commands need that and their man pages mention that explicitly.

Now about --, I have created a file -spam, let's cat the file:

$ echo foobar >-spam  

$ cat -spam         
cat: invalid option -- 'p'
Try 'cat --help' for more information.

$ cat -- -spam      
foobar

Without --cat takes spam all as it's options as they follow --- explicitly indicates the end of option(s), after that -spam is taken as a file name.

posted @ 2018-11-13 09:52  立志做一个好的程序员  阅读(285)  评论(0编辑  收藏  举报

不断学习创作,与自己快乐相处