为什么反斜杠防止别名扩展? [英] Why do backslashes prevent alias expansion?

查看:177
本文介绍了为什么反斜杠防止别名扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的问题的第一部分,我将提供一些背景信息作为一个
服务给社区。第二部分包含实际问题。



第一部分



假设我' ve创建了以下别名:

  alias ls ='ls -r'
/ pre>

我知道如何使用以下
方式临时unalias(即覆盖此别名):



1)命令的完整路径名: / bin / ls



2)命令替换: $(which ls)



3)命令内置:命令ls



4)双引号:ls



5)单引号:'ls'



6)反斜杠字符: \ls



案例1是明显的,情况2只是一个变体。案例3中的内置命令被设计为忽略shell函数,但显然它也可以用于规避别名。最后,案例4和5与 POSIX标准(2.3.1) )


确定的
的结果字是$ b $的命令名字b简单的命令将被检查到
确定它是否是一个无引号的
有效的别名。


Bash参考手册(6.6)


每个简单的
命令的第一个单词(如果未引用)被检查到
,看看它是否有一个别名。 / p>

第二部分



以下是问题:为什么案例6(通过说 \ls 覆盖别名)
考虑引用该词?根据这个问题的样式,我正在寻找对官方文档的引用。



文档说,反斜杠只能转义以下
字符,而不是单引号和双引号,引用一个
字符序列。 POSIX标准(2.2.1)


未引用的反斜杠
保留
以下字符的字面值,其中
为a< newline>


Bash参考手册(3.1.2.1)


非引号的反斜杠\是
Bash转义字符,它保留
下一个
字符的字面值,后面是
的换行符。


(BTW,不是下一个字符跟随一点点过度?)



可能的答案可能是这种情况不是那么特别:它是
类似于ANSI-C引用中的一些情况,例如 \\\
nn
。但是,这仍然是
转义单个字符(八位字符的值为八进制
值nnn),而不是字符序列。

解决方案

只是为了完成,这是另一种方法来抑制别名&函数查找(通过清除单个命令的整个shell环境):

 #cf. http://bashcurescancer.com/temporarily-clearing-environment-variables.html 
env -i ls


In the first part of my question I will provide some background info as a service to the community. The second part contains the actual question.

Part I

Assume I've created the following alias:

alias ls='ls -r'

I know how to temporarily unalias (i.e., override this alias) in the following ways, using:

1) the full pathname of the command: /bin/ls

2) command substitution: $(which ls)

3) the command builtin: command ls

4) double quotation marks: "ls"

5) single quotation marks: 'ls'

6) a backslash character: \ls

Case 1 is obvious and case 2 is simply a variation. The command builtin in case 3 was designed to ignore shell functions, but apparently it also works for circumventing aliases. Finally, cases 4 and 5 are consistent with both the POSIX standard (2.3.1):

"a resulting word that is identified to be the command name word of a simple command shall be examined to determine whether it is an unquoted, valid alias name."

and the Bash Reference Manual (6.6):

"The first word of each simple command, if unquoted, is checked to see if it has an alias."

Part II

Here's the question: why is case 6 (overriding the alias by saying \ls) considered quoting the word? In keeping with the style of this question, I am looking for references to the "official" documentation.

The documentation says that a backslash only escapes the following character, as opposed to single and double quotation marks, which quote a sequence of characters. POSIX standard (2.2.1):

"A backslash that is not quoted shall preserve the literal value of the following character, with the exception of a < newline >"

Bash Reference Manual (3.1.2.1):

"A non-quoted backslash ‘\’ is the Bash escape character. It preserves the literal value of the next character that follows, with the exception of newline."

(BTW, isn't "the next character that follows" a bit of overkill?)

A possible answer might be that this situation isn't that special: it is similar to a few cases in ANSI-C quoting, e.g. \nnn. However, that is still escaping a single character (the eight-bit character whose value is the octal value nnn), not a sequence of characters.

解决方案

Just for completion, here's yet another way to suppress alias & function lookups (by clearing the entire shell environment for a single command):

# cf. http://bashcurescancer.com/temporarily-clearing-environment-variables.html
env -i ls

这篇关于为什么反斜杠防止别名扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆