“-”有什么魔力? (破折号)在命令行参数中?
示例:
创建 ISO 映像并将其直接刻录到 CD。
mkisofs -V 照片 -r /home/vivek/photos | cdrecord -v dev=/dev/dvdrw -
切换到上一个目录。
cd -
监听端口 12345 并解压发送给它的数据。
<代码>nc -l -p 12345 | tar xvzf -
破折号的用途是什么以及如何使用它?
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 -
What is the purpose of the dash and how do I use it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
神奇之处就在于大会。几千年来,人们一直使用
-
作为前缀来区分选项和参数,并使用-
作为文件名来表示 stdin 或 stdout(视情况而定)。不要低估惯例的力量!The magic is in the convention. For millennia, people have used
-
as a prefix to distinguish options from arguments, and have used-
as a filename to mean either stdin or stdout, as appropriate. Do not underestimate the power of convention!它意味着使用程序的标准输入流
stdin
或标准输出流stdout(取决于上下文)。对于
cd
,它的含义有所不同:更改到上一个工作目录。It means to use the program's standard input stream
stdin
or standard output stream stdout (depending on the context).In the case of
cd
, it means something different: change to the previous working directory.-
确切地表示每个命令想要它表示的含义。有几种常见的约定,您已经在其他答案中看到了其中大多数的示例,但它们都不是 100% 通用的。就 shell 而言,
-
字符没有什么神奇之处(除了 shell 本身及其一些内置命令,例如cd
和echo
,以常规方式使用它)。有些字符,如\
、'
和"
, 是“神奇的”,无论出现在哪里都具有特殊含义这些是“shell 元字符”,但要了解给定命令如何使用
-
,请阅读该命令的文档。 。-
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.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 likecd
andecho
, 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.To see how a given command uses
-
, read the documentation for that command.这不是魔法。某些命令将
-
解释为用户想要从stdin
读取或写入stdout
。对于shell来说它没有什么特别的。
It's not magic. Some commands interpret
-
as the user wanting to read fromstdin
or write tostdout
.There is nothing special about it to the shell.
如果您指的是
tar
命令末尾的裸露-
,那么这在许多想要使用文件的命令中很常见。它允许您指定标准输入或输出,而不是实际文件名。
这就是您的第一个和第三个示例的情况。例如,
cdrecord
命令采用标准输入(由mkisofs
生成的 ISO 映像流)并将其直接写入/dev/dvdrw
。使用 cd 命令,每次更改目录时,它都会存储您原来的目录。如果您使用特殊的
-
“目录名称”执行cd
,它将使用记住的目录而不是真实的目录。通过使用它,您可以轻松地在两个目录之间快速切换。其他命令可能会将
-
视为不同的特殊值。If you mean the naked
-
at the end of thetar
command, that's common on many commands that want to use a file.It allows you to specify standard input or output rather than an actual file name.
That's the case for your first and third example. For example, the
cdrecord
command is taking standard input (the ISO image stream produced bymkisofs
) and writing it directly to/dev/dvdrw
.With the
cd
command, every time you change directory, it stores the directory you came from. If you docd
with 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.Other commands may treat
-
as a different special value.