Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape sequence in single quote is wrong? #6

Closed
sorawee opened this issue Jun 20, 2020 · 1 comment
Closed

Escape sequence in single quote is wrong? #6

sorawee opened this issue Jun 20, 2020 · 1 comment

Comments

@sorawee
Copy link

sorawee commented Jun 20, 2020

In shell:

$ echo '\n'


$ echo '\\n'
\n

So obviously shells see \n and \\n differently.

However, in your code:

  • For \n, when \ is encountered followed by n, it pushes both \ and n onto the result.
  • For \\n, when \ is encountered followed by \, it pushes \ onto the result. Then, it pushes n onto the result.

So it appears that they are treated in the same way, which as I understand, is incorrect.

@fenhl
Copy link
Collaborator

fenhl commented Feb 3, 2021

This gets somewhat more confusing since echo (or at least some implementations of it) looks at the shell lexing output and applies its own rules for escape sequences. Here's a summary of splitting behaviors, tested without relying on echo (and let's hope this table doesn't get scrambled by incompatible Markdown implementations — it reads as intended on the GitHub website):

Implementation Result for '\n' Result for '\\n'
bash 3.2 \n \\n
Zsh 5.8 \n \\n
Python shlex \n \\n
Rust shlex \n \n

So while none of these result in an actual newline, you're right about the Rust implementation being incorrect, regardless of which implementation is used as a reference point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants