${(q)…}: Quote shell characters

q escapes characters, which are special to the shell, with a backslash \.

There are several variants of this flag:

  • (q): Escaping of special characters, no quoting. Unprintable and invalid characters are shown as $'\NNN'.
  • (qq): Escaping of special characters, always quoting with single quotes
  • (q-): Escaping of special characters, minimal quoting with single quotes
  • (q+): Escaping of special characters, minimal quoting with unprintable characters shown as $'...'
  • (qqq): Escaping of special characters, always quoting with double quotes
  • (qqqq): Escaping of special characters, always quoting with $'' quotes

Examples

Using parameter name indirection
1
2
3
4
5
6
7
8
# contains newline and tab characters
tokens=('*' $'\b' $'\t' "\"" "'" are special to the shell)
echo "q:"    ${(q)tokens}
echo "qq:"   ${(qq)tokens}
echo "q-:"   ${(q-)tokens}
echo "q+:"   ${(q+)tokens}
echo "qqq:"  ${(qqq)tokens}
echo "qqqq:" ${(qqqq)tokens}
Output:
1
2
3
4
5
6
q: \* $' $'     ' \" \' are special to the shell
qq: '*' ' '     ' '"' ''\''' 'are' 'special' 'to' 'the' 'shell'
q-: '*' '       ' '"' \' are special to the shell
q+: '*' $'\C-H' $'      ' '"' \' are special to the shell
qqq: "*" " "    " "\"" "'" "are" "special" "to" "the" "shell"
qqqq: $'*' $' $'        ' $'"' $'\'' $'are' $'special' $'to' $'the' $'shell'
BashSupport Pro ist eine Zsh IDE mit Unterstützung für ${(q)…} - probieren Sie es jetzt aus!
© 2020–2024 Joachim Ansorg
Impressum
Datenschutz
Rechtliches