Bash: How to use the operator parameter extension $ {parameter @operator}?

I googled and tried so many things and could never get anything to work with $ {parameter @operator}. All I find is more links to the same documentation. Therefore, I believe that the correct answer with practical examples will be very useful for understanding it.

The documentation says:

$ {parameter @operator}

Decomposition is either a conversion of the value of a parameter or information about the parameter itself, depending on the value of the operator. Each statement is a single letter:

Q
An extension is a string that is a parameter value specified in a format that can be reused as an input.

It also talks about quoting:

3.1.2. Quoting is used to remove the special meaning of certain characters or words in a 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 like this (between "," \ "it is deleted, so for reuse you need to use for 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:

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

to no avail.

In fact, any operators I try to use always give a bad replacement error. These seem to be very obscure features of bash. I missed from this post about half an hour of attempts, each of which is more twisted than the previous one!

+4
source share
2 answers

, Stack , :

escape (\n)

$ foo=$(<file.txt)
$ echo "${foo@Q}"
$'line1\nline2'

, bash 4.4, < >

+2

, , , .

${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 xpanded (,\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

( escape-, . . bash documentation.)

${varname@A} , A. ssign , declare , .

:

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

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

${varname@A} , A.

:

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

( : -r readonly, -i integer, -a array, -f function, -x exportable.)

+2

Source: https://habr.com/ru/post/1661518/


All Articles