【发布时间】:2013-03-19 11:44:51
【问题描述】:
\curl -L https://get.rvm.io | bash -s stable
为什么命令以\ 开头?
This is the site where I saw it.
【问题讨论】:
\curl -L https://get.rvm.io | bash -s stable
为什么命令以\ 开头?
This is the site where I saw it.
【问题讨论】:
alias curl='curl --some --default --options'
如果您有 curl 的别名并且您不想使用它,则在前面加上反斜杠会禁用别名并直接运行 curl 二进制文件。
请注意,这只适用于交互式外壳。别名在脚本中不生效,因此在那里没有必要。
【讨论】:
shopt -s expand_aliases
\curl 不会绕过任何名为curl 的shell 函数。为此,您可以使用 bash 内置命令 command: command curl ...
\curl ...的更容易理解的方式是command curl ...
dash(可能还有其他 shell,尽管您对 bash 没有 expand_aliases 是正确的)确实在脚本中扩展别名。
The (Bourne/POSIX) shell specification 表示,当引用命令字的任何字符时,交互式 shell 中的 别名替换 将被禁止。反斜杠是这样做的一种方法,但还有其他众所周知的引用方式:单引号和双引号。以下所有内容都将禁止别名替换:
\curl
cur\l
\c\u\r\l
"c"url
"curl"
"c""u""r""l"
'curl'
'cu'"rl"
使用\curl 只是最常见和可读的方式。由于这是一个标准化功能,您可以期望它在所有 Bourne-heritage shell 中工作。
\curl 看起来有点像 TeX 命令,不是吗? :-)
【讨论】:
\curl 绕过同名别名的具体原因;请注意,只有 aliases 会以这种方式被绕过,而不是 shell 函数; command curl ... 将确保绕过 either。
command() { echo "Not command, lol!"; } ; command -V echo ; \command -V echo ; \command command echo "This is command! (masking despair)" 打印Not command, lol! x 3。
command itself 替换为一个shell 函数,你就是在破坏机制。如前所述,您的示例显示\ 不会绕过functions。一个不会弄巧成拙的例子:date() { echo 'not date'; }; date; command date。如果您担心对command 进行恶意篡改,请参阅stackoverflow.com/a/35931876/45375
command 不被覆盖。来自您自己的链接:Thus, with no control over the execution environment, you cannot write shell scripts that are fully immune to tampering, unless you know that your code will be executed by dash, ksh, or bash (with the workaround in place)