Linux 命令行参数中“-”(破折号)的神奇之处是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8045479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 07:05:31  来源:igfitidea点击:

What's the magic of "-" (a dash) in command-line parameters?

linuxbashshellunixcommand-line

提问by chemila

Examples:

例子:

  • Create an ISO image and burn it directly to a CD.

    mkisofs -V Photos -r /home/vivek/photos | cdrecord -v dev=/dev/dvdrw -

  • Change to the previous directory.

    cd -

  • Listen on port 12345 and untar data sent to it.

    nc -l -p 12345 | tar xvzf -

  • 创建 ISO 映像并将其直接刻录到 CD。

    mkisofs -V Photos -r /home/vivek/photos | cdrecord -v dev=/dev/dvdrw -

  • 切换到上一个目录。

    cd -

  • 侦听端口 12345 并解压缩发送给它的数据。

    nc -l -p 12345 | tar xvzf -

What is the purpose of the dash and how do I use it?

破折号的目的是什么,我该如何使用它?

采纳答案by paxdiablo

If you mean the naked -at the end of the tarcommand, that's common on many commands that want to use a file.

如果您指的-tar命令末尾的裸体,这在许多想要使用文件的命令中很常见。

It allows you to specify standard input or output rather than an actualfile name.

它允许您指定标准输入或输出而不是实际文件名。

That's the case for your first and third example. For example, the cdrecordcommand is taking standard input (the ISO image stream produced by mkisofs) and writing it directly to /dev/dvdrw.

您的第一个和第三个示例就是这种情况。例如,该cdrecord命令采用标准输入(由 生成的 ISO 映像流mkisofs)并将其直接写入/dev/dvdrw.

With the cdcommand, every time you change directory, it stores the directory you came from. If you do cdwith the special -"directory name", it uses that remembered directory instead of a real one. You can easily switch between two directories quite quickly by using that.

使用该cd命令,每次更改目录时,它都会存储您来自的目录。如果您cd使用特殊的-“目录名称”,它会使用记住的目录而不是真正的目录。通过使用它,您可以轻松地在两个目录之间快速切换。

Other commands may treat -as a differentspecial value.

其他命令可能会被-视为不同的特殊值。

回答by Brian Cain

It means to use the program's standard input stream.

意思是使用程序的标准输入流。

In the case of cd, it means something different: change to the prior working directory.

在 的情况下cd,它的含义有所不同:更改到先前的工作目录。

回答by Ignacio Vazquez-Abrams

It's not magic. Some commands interpret -as the user wanting to read from stdin or write to stdout; there is nothing special about it to the shell.

这不是魔法。一些命令解释-为用户想要从标准输入读取或写入标准输出;外壳没有什么特别之处。

回答by Ry-

Redirection from or to stdinor stdout.

重定向自 或 至stdinstdout

See: http://tldp.org/LDP/abs/html/special-chars.html#DASHREF2

请参阅:http: //tldp.org/LDP/abs/html/special-chars.html#DASHREF2

回答by William Pursell

The magic is in the convention. For millennia, people have used '-' to distinguish options from arguments, and have used '-' in a filename to mean either stdin or stdout, as appropriate. Do not underestimate the power of convention!

魔术在公约中。几千年来,人们一直使用“-”来区分选项和参数,并在文件名中使用“-”来表示标准输入或标准输出,视情况而定。不要低估约定俗成的力量!

回答by Keith Thompson

-means exactly what each command wants it to mean. There are several common conventions, and you've seen examples of most of them in other answers, but none of them are 100% universal.

-确切地表示每个命令想要它的意思。有几个常见的约定,您已经在其他答案中看到了其中大部分的示例,但没有一个是 100% 通用的。

There is nothing magic about the -character as far as the shell is concerned (except that the shell itself, and some of its built-in commands like cdand echo, use it in conventional ways). Some characters, like \, ', and ", are"magical", having special meanings wherever they appear. These are "shell metacharacters". -is not like that.

-就 shell 而言,角色并没有什么神奇之处(除了 shell 本身,以及它的一些内置命令,如cdand echo,以传统方式使用它)。某些字符,例如\'"“神奇的”,无论出现在何处都具有特殊含义。这些是“shell 元字符”。 -不是那样的。

To see how a given command uses -, read the documentation for that command.

要查看给定命令的使用方式-,请阅读该命令的文档