Bash: How to use operator parameter expansion ${parameter@operator}? Bash: How to use operator parameter expansion ${parameter@operator}? bash bash

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


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}"onetwo        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.)


Sorry, I didn't browse Stack properly, there is an example use here:

How to display a file with multiple lines as a single string with escape chars (\n)

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

Plus, I don't have bash 4.4 in which the feature was implemented < sigh >