Skip to content

Inconsistent backslash escaping in literal strings #1692

Open
@KrisShannon

Description

@KrisShannon

Describe the bug
Can't write a literal string with a single backslash in it. To obtain a string with a single backslash requires the use of other functions.

Version of yq: 4.33.3
Operating system: mac
Installed via: binary release

Command
The command you ran:

yq -nr eval '"\\"'
yq -nr eval '"\"'
yq -nr eval '"\\"|match("^(.)")|.string'

Actual behavior

\\
Error: 1:1: invalid input text "\"\\\""
\

Expected behavior

Either:

\
Error: 1:1: invalid input text "\"\\\""
\

Or:

\\
\
\

Additional context
It appears that the only backslash escape that is recognised is \n. There is no \r, \t, \b, \xHH or \uXXXX. All other alphabetics are left alone with the preceding backslash left in the string.

I also tried to use single quotes and backquotes in case they were alternate string literal expressions but they also just gave the invalid input text error.

The actual workaround I am currently using is:

BACKSLASH='\' yq -nr eval 'strenv(BACKSLASH)'

(It's actually a much more complicated expression that happens to include ... | split(strenv(BACKSLASH)) | ... as part of it)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions