[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sus behaviour when cmd string ends with single backslash

From: Chet Ramey
Subject: Re: Sus behaviour when cmd string ends with single backslash
Date: Mon, 14 Feb 2022 17:15:21 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.6.0

On 2/13/22 3:15 PM, vzvzvz2@gmail.com wrote:

Bash Version: 5.0
Patch Level: 17
Release Status: release



Commit a0c0a00fc419b7bc08202a79134fcd5bc0427071 (bash-4.4) introduced a change 
in parse.y with following documentation in the change logs:

        - shell_getc: if bash is reading input from a string that ends with an
          unquoted backslash, add another backslash instead of a newline, since
          the backslash and newline will disappear in normal processing.  Fixes
          bug with `bash -c 'eval \\; echo y' ' skipping the eval command and
          setting incorrect exit status, and `bash -ic 'eval \\; echo y' '
          seeing EOF on empty line and exiting before the echo.  Keep track of
          backslash state with last_was_backslash; set in char reading loop.
          Fixes bug reported by Eduardo A. Bustamante López <dualbus@gmail.com>

The new code in parse.y

          /* Don't add a newline to a string that ends with a backslash if we're
             going to be removing quoted newlines, since that will eat the
             backslash.  Add another backslash instead (will be removed by
             word expansion). */
          if (bash_input.type == st_string && expanding_alias() == 0 && last_was_backslash 
&& c == EOF && remove_quoted_newline)
            shell_input_line[shell_input_line_len] = '\\';
            shell_input_line[shell_input_line_len] = '\n';
          shell_input_line[shell_input_line_len + 1] = '\0';

This specific change is also there in commit 
0385211bb5cb01e0259c64ec2c5cc6337d4e215c on a development branch.

Observed vs. expected behaviour

The mentioned bug is indeed fixed by this change. However, in case of another 
edge case following new behaviour is observable:

  $ bash -c 'echo \'
  $ # backslash appears on output

I think the behavior of a word consisting only of a backslash is officially

It's not a quote character -- there's nothing to quote -- so it's not
removed by quote removal.

In general, I think it's better to err on the side of preserving output

Existing shell behavior varies, so you can't really count on anything.

``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]