Skip to content

Remove backslash-newline line continuations in shellsplit - #55

Open
afurm wants to merge 1 commit into
ruby:masterfrom
afurm:backslash-newline
Open

afurm wants to merge 1 commit into
ruby:masterfrom
afurm:backslash-newline

Conversation

@afurm

@afurm afurm commented Oct 16, 2026

Copy link
Copy Markdown
Member

Shellwords.split is documented to split "in the same way the UNIX Bourne shell does", but it doesn't handle a backslash-newline line continuation. The shell removes \<newline> both outside quotes and inside double quotes:

$ sh -c 'printf "[%s]" foo\
bar "x\
y"'
[foobar][xy]

Before:

Shellwords.split("foo\\\nbar")    # => ["foo\\\nbar"]
Shellwords.split("\"x\\\ny\"")    # => ["x\ny"]
Shellwords.split("foo \\\n bar")  # => ["foo", "\\\n", "bar"]

After:

Shellwords.split("foo\\\nbar")    # => ["foobar"]
Shellwords.split("\"x\\\ny\"")    # => ["xy"]
Shellwords.split("foo \\\n bar")  # => ["foo", "bar"]

A continuation on its own no longer produces a word, so "foo \\\n" gives ["foo"] and "\\\n" gives []. Single-quoted text and the escaped-newline output of shellescape ('\n') are unchanged, and I added tests for these cases.

The Bourne shell removes a backslash followed by a newline, both outside
quotes and inside double quotes. shellsplit kept the backslash and the
newline outside quotes, and kept the newline inside double quotes, so
"foo\\<newline>bar" became "foo\\\nbar" instead of "foobar".
@afurm
afurm requested a review from knu as a code owner October 16, 2026 17:05

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant