'bash -c'是什么意思?

155
我遵循了以下教程:http://davidtsadler.com/archives/2012/06/03/how-to-install-magento-on-ubuntu/
在某个时刻,它告诉我执行以下命令:
sudo bash -c "cat >> /etc/apache2/sites-available/magento-store.com <<EOF
<VirtualHost *:80>

  ServerName  localhost.magento-store.com
  ServerAlias www.localhost.magento-store.com

  DocumentRoot /home/dev/public_html/magento-store.com/public

  LogLevel warn
  ErrorLog  /home/dev/public_html/magento-store.com/log/error.log
  CustomLog /home/dev/public_html/magento-store.com/log/access.log combined

</VirtualHost>
EOF"

这个命令做了什么,我该如何取消它?

我重新启动了电脑,但似乎它仍在运行。我查看了.bashrc.profile,但没有找到它。


http://www.gnu.org/software/bash/manual/bashref.html#Invoking-Bash - Oliver Charlesworth
该命令应该立即运行并完成。要撤消它,您需要编辑“magento-store.com”文件。 - Flimm
http://davidtsadler.com/archives/2012/06/03/how-to-install-magento-on-ubuntu/ 似乎已经失效。无论是 davidtsadler.com 还是 www.davidtsadler.com 都无法在 DNS 中找到。 - Peter Mortensen
这个问题被提出时的链接页面是 https://web.archive.org/web/20131128063029/https://davidtsadler.com/archives/2012/06/03/how-to-install-magento-on-ubuntu/。 - undefined
3个回答

142

引用自man bash

-c string 如果存在-c选项,则从string中读取命令。
如果字符串后面有参数,则将它们分配给位置参数,以$0开头。

您引用的命令将追加heredoc中的文本(即VirtualHost标记中的文本)到文件/etc/apache2/sites-available/magento-store.com中。


36
没有使用“-c”和使用“-c”有什么不同? - voices
40
实际上,如果在这种情况下不使用 -c 选项,你会花费数小时进行调试,试图以各种方式避免各种引号,并且仍然无法正常工作。 - labyrinth
8
请注意,从bash -c调用的命令将被强制执行单线程。例如:"bash -c 'ffmpeg -threads 4 -i input.mov output.mp4'"会调用ffmpeg,但即使您指定了"threads"参数,它也只能使用一个核心。请注意不改变原文意思,并使翻译易于理解,不要添加任何额外内容。 - Christian
3
@Christian 为什么?那似乎非常不合理。 - flaviut
14
@Christian 这种行为没有记录,并且它也与实际行为不符:https://i.imgur.com/ZeFAkDf.png - flaviut
显示剩余5条评论

19

Bash的手册页面(例如man bash)说-c选项执行来自字符串的命令,即引号中的所有内容。


5

请查看man手册,可以在您的计算机上或者互联网上查找,比如这个

引用:

-c string
     If the -c option is present, then commands are read from string.
     If there are arguments after the string, they are assigned to the positional
     parameters, starting with $0.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接