Bash:如何使用运算符参数扩展$ {parameter @ operator}? [英] Bash: How to use operator parameter expansion ${parameter@operator}?

查看:120
本文介绍了Bash:如何使用运算符参数扩展$ {parameter @ operator}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Google上搜索并尝试了很多东西,但是从来没有任何东西可以与$ {parameter @ operator}一起使用.我发现的只是指向相同文档的更多链接.因此,我认为适当地举一些实际例子将有助于其理解.

文档说:

$ {parameter @ operator}

扩展是对 参数的值或有关参数本身的信息,取决于 运算符的值.每个运算符都是一个字母:

Q
扩展名是一个字符串,它是在 可以重复用作输入的格式.

它也说明了引用:

3.1.2报价
引号用于删除某些字符或单词在shell中的特殊含义.

所以我的推理是这个(特殊字符$)的输出:

a="To be reused as an input string, \$0 needs to be quoted"
echo ${a@Q}

应该是这样(在"之间,"\"已删除,因此需要再次引用以用作输入):

To be reused as an input string, \$0 needs to be quoted

但是我得到了

bash: ${a@Q}: bad substitution

我尝试了以下几种组合:

${a@q}, "${a@Q}", a='To be reused as an input string, $0 needs to be quoted'

无济于事.

实际上,我尝试使用的任何运算符总是会产生错误的替换错误.这些似乎是bash非常晦涩的特征.我从这篇文章中省略了大约半小时的尝试,每次尝试都比前一次更加扭曲!

解决方案

对于那些到达这里寻求有关其他扩展运算符的信息的人,这里是可用扩展字符及其作用的快速列表.

${varname@Q} 返回单个 Q 字符串,其中转义了任何特殊字符(例如\ n,\ t等).

示例:

$ foo="one\ntwo\n\tlast"
$ echo "$foo"
one\ntwo\n\tlast

$ echo ${foo@Q}
'one\ntwo\n\tlast'

${varname@E} 返回一个包含所有转义字符 E x的字符串(例如\ n->换行符).

示例:

$ foo="one\ntwo\n\tlast"
$ echo "${foo}"
one\ntwo\n\tlast

$ echo "${foo@E}"
one
two
        last

${varname@P} 返回一个字符串,该字符串显示该变量用作 P rompt变量(即PS1,PS2,PS3)时的外观 >

示例:

$ bar='host: \h'
$ echo ${bar@P}
host: myhost1

(还有更多可用于提示变量的转义序列.请参见

${varname@a} 返回一个字符串,其中列出了变量的 A 贡献.

示例:

$ declare -ir foo=10
$ echo ${foo@a}
ir

(可用的声明选项为:-r只读,-i整数,-a数组,-f函数,-x可导出.)

I've googled and tried so many things and never could get anything to work with ${parameter@operator}. All I find is more links to the same documentation. So I think a proper answer with practical examples would be very helpful to its understanding.

The documentation says:

${parameter@operator}

The expansion is either a transformation of the value of parameter or information about parameter itself, depending on the value of operator. Each operator is a single letter:

Q
The expansion is a string that is the value of parameter quoted in a format that can be reused as input.

It also says about quoting:

3.1.2 Quoting
Quoting is used to remove the special meaning of certain characters or words to the shell.

So my reasoning is that the output of this (special character $):

a="To be reused as an input string, \$0 needs to be quoted"
echo ${a@Q}

should be this (between "", the '\' is removed, so needs quoting again to be used as an input):

To be reused as an input string, \$0 needs to be quoted

but I get:

bash: ${a@Q}: bad substitution

I tried different combinations of:

${a@q}, "${a@Q}", a='To be reused as an input string, $0 needs to be quoted'

to no avail.

Actually, any operators I try to use always yields the bad substitution error. Those seems to be very obscure features of bash. I've omitted from this post about half an hour of attempts each one more twisted than the previous!

解决方案

For those who arrive here looking for information on a different expansion operator, here's a quick list of the available expansion characters and their effects.

${varname@Q} returns a single-Quoted string with any special characters (such as \n, \t, etc.) escaped.

Examples:

$ foo="one\ntwo\n\tlast"
$ echo "$foo"
one\ntwo\n\tlast

$ echo ${foo@Q}
'one\ntwo\n\tlast'

${varname@E} returns a string with all the escaped characters Expanded (e.g. \n -> newline).

Examples:

$ foo="one\ntwo\n\tlast"
$ echo "${foo}"
one\ntwo\n\tlast

$ echo "${foo@E}"
one
two
        last

${varname@P} returns a string that shows what the variable would look like if it were used as a Prompt variable (i.e. PS1, PS2, PS3)

Example:

$ bar='host: \h'
$ echo ${bar@P}
host: myhost1

(There are many more escape sequences that can be applied in prompt variables. See the bash documentation.)

${varname@A} returns a string that can be used to Assign the variable with its existing name, value, and declare options if any.

Example:

$ foo="test1"
$ echo ${foo@A}
foo='test1'

$ declare -i foo=10
$ echo "${foo@A}"
declare -i foo='10'

${varname@a} returns a string listing the Atributes of the variable.

Example:

$ declare -ir foo=10
$ echo ${foo@a}
ir

(The available declare options are: -r readonly, -i integer, -a array, -f function, -x exportable.)

这篇关于Bash:如何使用运算符参数扩展$ {parameter @ operator}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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