bash 为什么用反斜杠启动 shell 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15691977/
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-09-09 23:31:29  来源:igfitidea点击:

Why start a shell command with a backslash?

bashshell

提问by lbaby

\curl -L https://get.rvm.io | bash -s stable

Why is the command starting with \? This is the site where I saw it.

为什么命令以\? 这是我看到它的网站。

回答by John Kugelman

alias curl='curl --some --default --options'

If you have an alias for curland you don't want to use it, putting a backslash in front disables the alias and runs the curl binary directly.

如果您有别名curl但不想使用它,则在前面放置反斜杠会禁用别名并直接运行 curl 二进制文件。

Note that this only applies at an interactive shell. Aliases don't take effect in scripts so it would be unnecessary there.

请注意,这仅适用于交互式 shell。别名在脚本中不起作用,因此在那里没有必要。

回答by Jens

The (Bourne/POSIX) shell specificationsays that alias substitutionin an interactive shell is suppressed when any character of the command word is quoted. A backslash is one way to do that, but there are also other well known ways to quote: single and double quotes. All of the following will suppress alias substitution:

(Bourne/POSIX) shell 规范说,当命令字的任何字符被引用时,交互式 shell中的别名替换将被抑制。反斜杠是一种方法,但还有其他众所周知的引用方法:单引号和双引号。以下所有内容都将禁止别名替换:

 \curl
 cur\l
 \c\u\r\l
 "c"url
 "curl"
 "c""u""r""l"
 'curl'
 'cu'"rl"

Using \curlis just the most common and readable way. Since this is a standardized feature, you can expect it to work in all Bourne-heritage shells.

使用\curl只是最常见和可读的方式。由于这是一项标准化功能,因此您可以期望它在所有 Bourne 继承的 shell 中工作。

\curllooks a bit like a TeX command, doesn't it? :-)

\curl看起来有点像 TeX 命令,不是吗?:-)