Skip to content

Unexpected escaping of quotes when expanding variable to external command #6465

Closed

Description

@MichelMunoz

Describe the bug

Edit:
See the comment further down #6465 (comment) for the issue with current version and quote / paths vs nu command.


Old description:
Despite #2095, in some cases quotes are still removed, thus breaking external commands using them for paths for example.

How to reproduce

If you execute this :

let exe = "^echo"
let somepath = "C:\\Program Files (x86)\\Realtek"
let command = $"($exe) --arg1 \"($somepath)\" -z@\"($somepath)\""
echo $"COMMMAND: ($command)"
nu -c $command

you obtain this :

COMMMAND: ^echo --arg1 "C:\Program Files (x86)\Realtek" -z@"C:\Program Files (x86)\Realtek"
Error: nu::parser::unclosed_delimiter (link)

  × Unclosed delimiter.
   ╭─[source:1:1]
 1 │ ^echo --arg1 C:\Program Files (x86)\Realtek" -z@"C:\Program Files (x86)\Realtek
   ·                                                            ▲
   ·                                                            ╰── unclosed )
   ╰────

after arg1 one double-quote for the path is removed, and after -z one double-quote is removed, for some reason.

Note : The "why" the command is in a variable ($exe) is because the command is known at runtime (depending on OS)

Expected behavior

per #2095 none of the paths double quote should be removed.
The command that should be execute would be what the echo $"COMMMAND: ($command)" shows

Screenshots

N/A

Configuration

nu version 0.66.2
tested on windows

Additional context

No response

Activity

  1. added
    category:bug
    A:external-commands
    A:quoting-expansion
    on Sep 2, 2022
  2. sholderbach commented on Sep 2, 2022

    @sholderbach
    Member

    Have you given the current release 0.67 a try?

  3. added
    status:more-information-needed
    on Sep 2, 2022
  4. MichelMunoz commented on Sep 5, 2022

    @MichelMunoz
    ContributorAuthor

    The result with 0.67 is:

    COMMMAND: ^echo --arg1 "C:\Program Files (x86)\Realtek" -z@"C:\Program Files (x86)\Realtek"
    --arg1 "\"C:\Program Files (x86)\Realtek\"" "-z@\"C:\Program Files (x86)\Realtek\""
    

    which seems to work (at least if we execute the command we obtain the same result).
    (maybe a side effect of #6154)

  5. MichelMunoz commented on Sep 5, 2022

    @MichelMunoz
    ContributorAuthor

    There is something wrong though, with the initial example, simplified :

    let exe = "^echo"
    let somepath = "X"
    let command = $"($exe) \"($somepath)\""
    echo $"COMMMAND: ($command)"
    nu -c $command
    

    will produce output:

    COMMMAND: ^echo "X"
    \"X\"           #<--- notice the back slashes here
    

    but if you use the underlying shell (here windows) and type echo "X" you get the expected "X" and not what nushell produces...

  6. MichelMunoz commented on Sep 12, 2022

    @MichelMunoz
    ContributorAuthor

    Given my last comment, ie. what is the real problem with the current version, should I

    • rename the ticket to match the real problem
    • create another one with the problem
      ?
  7. removed
    status:more-information-needed
    on Sep 12, 2022
  8. sholderbach commented on Sep 12, 2022

    @sholderbach
    Member

    I was going through the other open issues in the quoting field right now but did not find a direct match to your more recent problem at a first glance. So definitely keep it around. Moving new issue for the particular problem might be a bit more discoverable (but I don't want to cause you extra work) Thanks for the easy reproducible example.

    Title suggestion:
    Unexpected escaping of quotes when expanding variable to external command.

  9. changed the title [-]Double-quotes sill removed for external commands (related to #2095)[/-] [+]Unexpected escaping of quotes when expanding variable to external command[/+] on Sep 14, 2022
  10. MichelMunoz commented on Sep 14, 2022

    @MichelMunoz
    ContributorAuthor

    Title suggestion:
    Unexpected escaping of quotes when expanding variable to external command

    Ok, done (+ minor edit of first comment to redirect to the relevant comment).

  11. WindSoilder commented on Sep 15, 2022

    @WindSoilder
    Contributor

    Thanks @rgwood mentioned about what happened in detail:

    So this is what happens:

    • Nushell tries to run an executable called echo.exe but it can't find one
    • Because ECHO is one of the known cmd.exe internal commands, it tells cmd.exe to run the command
  12. added a commit that references this issue on May 23, 2024
  13. kronberger-droid commented on Nov 13, 2026

    @kronberger-droid
    Contributor

    External argument handling got rewritten in #12921 and quotes are no longer backslash-escaped on the cmd.exe path.
    I could only run this on Linux, so please reopen with the output of a current Windows build if ^echo "X" still prints \"X\".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:external-commandsA:quoting-expansioncategory:bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions