Supondo que:
$ value=$'This isn\'t a \n\x1b "correct" test'
$ printf '%s\n' "$value"
This isn't a
"correct" test
posix quote () { printf %s\n "$1" | sed "s/'/'\\''/g;1s/^/'/;\$s/\$/'/" ; }
Uso:
$ quote "${value}"
'This isn'\''t a
"correct" test'
This function simply replaces every instance of «
'
» (single quote) within the string with «'\''
» (single quote, backslash, single quote, single quote), then puts single quotes at the beginning and end of the string. Since the only character whose meaning is special within single quotes is the single quote character itself, this is totally safe. Trailing newlines are handled correctly, and the single quote at the end doubles as a safety character to prevent command substitution from clobbering the trailing newlines, should one want to do something like:quoted=$(quote "$var")
Atenção: os caracteres ESC (\ 033 ou \ x1b ou decimal 27) acima são citados (tecnicamente), mas são invisíveis. Quando enviado para um terminal, como outros personagens de controle, pode até causar danos. Somente quando eles são apresentados visualmente como $ '\ 033', $ '\ C- [' ou $ '\ E', eles são claramente visíveis e não ambíguos.
bash printf '%s\n' "${value@Q}" $'This isn\'t a \n\E "correct" test'
zsh printf '%s\n' ${(q)value} This\ isn\'t\ a\ $'\n'$'3'\ \"correct\"\ test
zsh printf '%s\n' ${(qq)value} 'This isn'\''t a
"correct" test'
zsh printf '%s\n' ${(qqq)value} "This isn't a
\"correct\" test"
zsh printf '%s\n' ${(qqqq)value} $'This isn\'t a \n3 "correct" test'
zsh printf '%s\n' ${(q-)value} 'This isn'\''t a
"correct" test'
zsh printf '%s\n' ${(q+)value} $'This isn\'t a \n\C-[ "correct" test'
Tenha cuidado com algumas strings entre aspas: os caracteres ESC (\ 033 ou \ x1b ou decimal 27) acima são todos (tecnicamente) citados, mas invisíveis. Quando enviado para um terminal, como outros personagens de controle, pode até causar danos. Somente quando eles são apresentados visualmente como $ '\ 033', $ '\ C- [' ou $ '\ E', eles são claramente visíveis e não ambíguos.
Em manual do Bash :
${parameter@operator}
Q
The expansion is a string that is the value of parameter quoted in a format that can be reused as input.
Na zshexpn
man page :
q
Quote characters that are special to the shell in the resulting words with backslashes; unprintable or invalid characters are quoted using the$'\NNN'
form, with separate quotes for each octet.If this flag is given twice, the resulting words are quoted in single quotes and if it is given three times, the words are quoted in double quotes; in these forms no special handling of unprintable or invalid characters is attempted. If the flag is given four times, the words are quoted in single quotes preceded by a
$
. Note that in all three of these forms quoting is done unconditionally, even if this does not change the way the resulting string would be interpreted by the shell.If a
q-
is given (only a singleq
may appear), a minimal form of single quoting is used that only quotes the string if needed to protect special characters. Typically this form gives the most readable output.If a
q+
is given, an extended form of minimal quoting is used that causes unprintable characters to be rendered using$'...'
. This quoting is similar to that used by the output of values by the typeset family of commands.