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

  1. $ echo foobar | cat -

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

  1. $ echo foobar >-spam

  2. $ cat -spam

  3. cat: invalid option -- 'p'

  4. Try 'cat --help' for more information.

  5. $ cat -- -spam

  6. foobar

Without --, cat takes s, p, a, m 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 on 2020-03-31 11:27  katago  阅读(153)  评论(0编辑  收藏  举报