为什么 sh.exe 会吞掉 `\` 以及如何转义 `\`?

why does sh.exe swallow `\` and how to escape `\`?

sh -c 'ls C:\Users\timothee'

给出:

ls: 无法访问 'C:Userstimothee': 没有那个文件或目录

sh -c 'ls C:\Users\timothee'

给出: ls: 无法访问 'C:Userstimothee': 没有那个文件或目录

这些工作:

sh -c 'ls C:\\Users\\timothee'
sh -c 'ls C:/Users/timothee'

但是有没有更好的办法呢?

我正在尝试使用 CreateProcess 以编程方式调用 shell(bash 或 sh)命令并正确地转义它,但是反斜杠的奇怪吞咽使这变得尴尬。请参阅下面的 C 示例:

这是下面最好的方法吗,使用 6(!) 个反斜杠? (我的完整程序必须将输入例如 echo C:\Users\timothee 转换为: echo C:\\\Users\\\timothee)

#ifdef _WIN32
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <cstdlib>
#include <string>
#include <algorithm>

#include <stdio.h>

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

void _tmain2(TCHAR *argv)
{
    // adapted from 
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        argv,        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}

int main (int argc, char *argv[]) {
  //char s[] = "echo ok1 && echo ok2"; // bad (prints: ok1 && echo ok2)
  //char s[] = "sh -c 'echo ok1 && echo ok2'"; // ok: prints ok1\nok2
  //char s[] = "sh -c 'echo C:\Users\timothee\'"; // error
  //char s[] = "sh -c 'echo C:/Users/timothee'"; // ok but I want \, not / as some windows program don't understand /
  //char s[] = "sh -c 'echo C:\\Users\\timothee'"; // BUG: prints: C:Userstimothee
  char s[] = "sh -c 'echo C:\\\Users\\\timothee'"; // prints: C:\Users\timothee
  _tmain2(s);

  return 0;
}

#endif //win32

链接:

问题是反斜杠需要转义两次:

  • 首先在 shell 调用 sh 命令
  • 然后由 sh 命令本身进行第二次。

所以当你使用命令行时

sh -c 'ls C:\\Users\\timothee'

运行 shell 将删除一个反斜杠并将 'ls C:\Users\timothee 传递给 sh 命令。

sh 命令在其自身的处理中需要双反斜杠,并将 C:\Users\timothee 传递给 ls 命令。