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

gives:

ls: cannot access 'C:Userstimothee': No such file or directory

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

gives: ls: cannot access 'C:Userstimothee': No such file or directory

these work:

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

but isn't there a better way?

I'm trying to programmatically call a shell (bash or sh) command using CreateProcess and escape it properly, but the weird swallowing of backslash makes this awkward. See C example below:

is this the best way below, using 6(!) backward slashes? (my full program would have to convert an input eg echo C:\\Users\\timothee to this: 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 https://stackoverflow.com/questions/42531/how-do-i-call-createprocess-in-c-to-launch-a-windows-executable
    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

links:

timotheecour
  • 3,104
  • 3
  • 26
  • 29

1 Answers1

1

The problem is that the back-slash needs to be escaped twice:

  • First by the shell where you invoke the sh command
  • Then a second time by the sh command itself.

So when you use the command-line

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

the running shell will remove one back-slash and pass 'ls C:\\Users\\timothee to the sh command.

The sh command in turn needs that double back-slash in its own processing, and will pass C:\Users\timothee to the ls command.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621