3.1.4.2 mppopm Filter Quoting

The non-terminal qtext appears frequently in the mpdpopm filter grammar (see mppopm Filter Syntax). These textual tokens are required to be enclosed in quotation marks, either by single-quote or a double-quote characters (ASCII characters 39 or 34, respectively; cf. ExpectQuoted in the MPD codebase) This brings up the question of how to represent single- or double-quotes inside the quoted text. MPD employs the time-honored tradition of “escaping” them with a backslash character (ASCII character 92). This then implies that backslashes in the quoted text must themselves be backslash-escaped.

For example, to test against the text

foo\bar''

using double-quotes, we would enclose the text in " characters, and backslash-escape any ", ', and \ characters therein, to obtain

"foo\\bar\""

We could also choose to quote the text using single-quotes in the same manner:

'foo\\bar\"'

Tragically, to use this text in a filter in the shell with mppopm, we will need to again quote the text in order to protect special characters therein from the shell.

Let us suppose we had songs by the pathologically named artist foo\bar”. A properly quoted filter expression would be:

(artist == "foo\\bar\"")

To invoke this on the command line, however, we would need to quote the filter itself (to make it into a single argument for mppopm), which would require quoting the entire filter:

mppopm findadd "(artist =~ \"foo\\\\bar\\\"\")"

In this particular case, quoting the filter using single-quotes might be simpler

mppopm findadd '(artist == "foo\\bar\"")'

If the artist name contained a single-quote, e.g.

foo'bar

We could quote it using "s

(artist =~ "foo\'bar")

but to express this in the shell we would need to protect the single-quote from bash:

mppopm findadd '(artist =~ "foo\'"'"'bar")'

Quoting the artist name using 's, and protecting that from the shell is left as an exercise for the reader.