[英]what does prefixed exclamation mark (`!`) command mean/do in shell?
We usually use $
as prompt in code block to demonstrate shell commands:我们通常在代码块中使用
$
作为提示符来演示 shell 命令:
$ echo "hello world" > foo.md
$ cat foo.md
hello world
The problem is that you can't copy and paste those two lines with prefixed $
into your Terminal and execute them.问题是您无法将带有前缀
$
的这两行复制并粘贴到终端中并执行它们。
bash: $: command not found
However, I accidentally found that you can use prefixed !
但是,我偶然发现你可以使用前缀
!
for your commands (and prefixed #
for outputs as comments)用于您的命令(并以
#
为前缀作为注释的输出)
! echo "hello world" > foo.md
! cat foo.md
# hello world
! ls
Now you can copy and paste above 4 lines into your shell and execute them at once.现在您可以将以上 4 行复制并粘贴到您的 shell 中并立即执行它们。
Is that true?真的吗? Is it worth to use
!
值得用
!
for demonstration within code blocks in markdown files?在 markdown 文件中的代码块中进行演示?
What does prefixed exclamation mark ( !
) command mean/do in shell? shell 中前缀感叹号 (
!
) 命令的含义/作用是什么?
As @chepner mentions below, the standalone !
正如@chepner 下面提到的,独立的
!
is acting as a logical not operator.充当逻辑非运算符。
Beyond that, traditionally the $
prefix indicates that the command should be run as an unprivileged user and the #
prefix indicates that the command should run as a privileged user (root).除此之外,传统上
$
前缀表示该命令应作为非特权用户运行,而#
前缀表示该命令应作为特权用户 (root) 运行。 See this answer for some more info.有关更多信息,请参阅此答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.