代码之家  ›  专栏  ›  技术社区  ›  steamer25

如何在行继续之后正确地转义带引号的命令?

  •  3
  • steamer25  · 技术社区  · 14 年前

    echo foo &&^
    "C:\Program Files\Internet Explorer\iexplore.exe"
    

    …产生以下输出:

    foo
    '"C:\Program' is not recognized as an internal or external command,
    operable program or batch file.
    

    我错过了什么?

    编辑

    如标签所示,此问题涉及Windows(XP)批处理文件。

    6 回复  |  直到 14 年前
        1
  •  7
  •   jeb    14 年前

    对不起,几乎所有的答案都是错的,或者不明白会发生什么。

    问题与cmd行无关,或者它是否在批处理文件中。



    这就是为什么你

    echo foo ^
    && echo bar
    ---- OUTPUT ---
    foo &
    bar
    ---------------
    
    rem equivalent to
    echo foo ^&& echo bar
    rem So the first ampersand is escaped and the second works as a single &
    

    这也是原始代码无法工作的原因

    echo foo &&^
    "C:\Program Files\Internet Explorer\iexplore.exe"
    
    rem is equivalent to this, so the quote will be escaped
    echo foo &&^"C:\Program Files\Internet Explorer\iexplore.exe"
    

    为什么另一种解决方案有效?

    echo foo &&^
    cmd /c "C:\Program Files\Internet Explorer\iexplore.exe"
    
    rem is equivalent to
    echo foo &&^cmd /c "C:\Program Files\Internet Explorer\iexplore.exe"
    

    是否转义“c”对于“cmd”并不重要。

    echo foo && >con ^
    "C:\Program Files\Internet Explorer\iexplore.exe"
    

    特例中的特例 ,插入符号作为普通多行使用,但不转义第一个字符(我想,cmd.exe是没有编程的,它来自混乱)。

        2
  •  1
  •   sakra    14 年前

    如果引号前面有条件处理符号(即。, & && , || ).

    echo foo &&^
    C:\Progra~1\Intern~1\iexplore.exe
    

    或者使用具有延迟变量展开的辅助变量:

    setlocal enabledelayedexpansion
    
    set "IE_EXE=C:\Program Files\Internet Explorer\iexplore.exe"
    
    echo foo &&^
    !IE_EXE!
    
        3
  •  1
  •   wimh    14 年前

    这似乎也奏效了:

    echo foo &&^
    cmd /c "C:\Program Files\Internet Explorer\iexplore.exe"
    
        4
  •  0
  •   bobs    14 年前

    echo foo ^
    && "C:\Program files\internet explorer\iexplore.exe"
    

    编辑:如问题所问,此命令必须在批处理文件中运行。它在批处理文件中工作,但在命令行中工作不正确!

        5
  •  -1
  •   DanP    14 年前

    如果路径包含空格,则需要引用该路径;我不太清楚你在用什么语言,但基本上你需要:

    ""C:\Program Files\Internet Explorer\iexplore.exe""
    
        6
  •  -1
  •   kmfk    14 年前

    尝试删除符号。

    echo foo ^ 
    "C:\Program Files\Internet Explorer\iexplore.exe"
    

    C:\Users\keith\Desktop>echo foo
    foo
    
    C:\Users\keith\Desktop>"C:\Program Files\Internet Explorer\iexplore.exe"
    

    @echo off 在批处理文件的顶部。