How wt.exe process the command line arguments with double quotes? #12768

Closed
opened 2026-01-31 03:24:25 +00:00 by claunia · 1 comment
Owner

Originally created by @lrtfm on GitHub (Mar 1, 2021).

I would like to add an "open with vim in wsl" item for files when right-clicking it. Sometimes, the file path or name would have space, so I must enclose the parameter with double-quotes. After trying many many times, I found the command

wt nt wsl -d Ubuntu-20.04 -e bash --login -c -v "vim \"""$(wslpath 'D:\Source Insight 4.0\test.txt')"""""""

which gives me the correct behavior. As you can see there is a space in the path D:\Source Insight 4.0\test.txt.

I was wondering why I must put \""" in before $( to give a " in the bash command while I should put multiple " after ) to get a ".

Could someone give me an explanation?

The windows terminal version is 1.5.10411.0
The cmd version is

Microsoft Windows [版本 10.0.19042.804]
Originally created by @lrtfm on GitHub (Mar 1, 2021). I would like to add an "open with vim in wsl" item for files when right-clicking it. Sometimes, the file path or name would have space, so I must enclose the parameter with double-quotes. After trying many many times, I found the command ``` wt nt wsl -d Ubuntu-20.04 -e bash --login -c -v "vim \"""$(wslpath 'D:\Source Insight 4.0\test.txt')""""""" ``` which gives me the correct behavior. As you can see there is a space in the path `D:\Source Insight 4.0\test.txt`. I was wondering why I must put `\"""` in before `$(` to give a `"` in the bash command while I should put multiple `"` after `)` to get a `"`. Could someone give me an explanation? The windows terminal version is `1.5.10411.0` The cmd version is ``` Microsoft Windows [版本 10.0.19042.804] ``` <!-- ``` ❯ [Environment]::OSVersion Platform ServicePack Version VersionString -------- ----------- ------- ------------- Win32NT 10.0.19042.0 Microsoft Windows NT 10.0.19042.0 ``` -->
claunia added the Needs-TriageNeeds-Tag-Fix labels 2026-01-31 03:24:25 +00:00
Author
Owner

@lrtfm commented on GitHub (Mar 9, 2021):

I found some info about this and put them here.

  1. CommandLineToArgvW is used to process the command line parameters. 83f2a3bb3d/src/cascadia/WindowsTerminal/AppHost.cpp (L120)
  2. Parsing C++ command-line arguments
// argument_definitions.cpp
// compile with: /EHsc
#include <iostream>
#include <string.h>

using namespace std;
int main( int argc, char *argv[], char *envp[] )
{
    bool numberLines = false;    // Default is no line numbers.

    // If /n is passed to the .exe, display numbered listing
    // of environment variables.
    if ( (argc == 2) && _stricmp( argv[1], "/n" ) == 0 )
         numberLines = true;

    // Walk through list of strings until a NULL is encountered.
    for ( int i = 0; envp[i] != NULL; ++i )
    {
        if ( numberLines )
            cout << i << ": "; // Prefix with numbers if /n specified
        cout << envp[i] << "\n";
    }
}
  1. CommandLineToArgvW function (shellapi.h)
#include <windows.h>
#include <stdio.h>
#include <shellapi.h>

int __cdecl main()
{
   LPWSTR *szArglist;
   int nArgs;
   int i;

   szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
   if( NULL == szArglist )
   {
      wprintf(L"CommandLineToArgvW failed\n");
      return 0;
   }
   else for( i=0; i<nArgs; i++) printf("%d: %ws\n", i, szArglist[i]);

// Free memory allocated for CommandLineToArgvW arguments.

   LocalFree(szArglist);

   return(1);
}

The output of the above code for args "vim \"""$(wslpath 'D:\my path\my file.txt')"""""" are

  • For code in list 2
C:\> echo_args_c.exe "vim \"""$(wslpath 'D:\my path\my file.txt')""""""

Command-line arguments:
  argv[0]   echo_args_c.exe
  argv[1]   vim ""$(wslpath 'D:\my path\my file.txt')"""
  • For code in list 3
C:\Users\CSAR>echo_args.exe "vim \"""$(wslpath 'D:\my path\my file.txt')""""""
0: echo_args.exe
1: vim ""$(wslpath
2: 'D:\my
3: path\my
4: file.txt')""

If you put two backslashes in the command line, the result will also be ok.

@lrtfm commented on GitHub (Mar 9, 2021): I found some info about this and put them here. 1. `CommandLineToArgvW` is used to process the command line parameters. https://github.com/microsoft/terminal/blob/83f2a3bb3dbb00a6e36aa29cd7dba01060254666/src/cascadia/WindowsTerminal/AppHost.cpp#L120 2. [Parsing C++ command-line arguments](https://docs.microsoft.com/en-us/cpp/cpp/main-function-command-line-args?view=msvc-160#parsing-c-command-line-arguments) ``` // argument_definitions.cpp // compile with: /EHsc #include <iostream> #include <string.h> using namespace std; int main( int argc, char *argv[], char *envp[] ) { bool numberLines = false; // Default is no line numbers. // If /n is passed to the .exe, display numbered listing // of environment variables. if ( (argc == 2) && _stricmp( argv[1], "/n" ) == 0 ) numberLines = true; // Walk through list of strings until a NULL is encountered. for ( int i = 0; envp[i] != NULL; ++i ) { if ( numberLines ) cout << i << ": "; // Prefix with numbers if /n specified cout << envp[i] << "\n"; } } ``` 3. [CommandLineToArgvW function (shellapi.h)](https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw) ``` #include <windows.h> #include <stdio.h> #include <shellapi.h> int __cdecl main() { LPWSTR *szArglist; int nArgs; int i; szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs); if( NULL == szArglist ) { wprintf(L"CommandLineToArgvW failed\n"); return 0; } else for( i=0; i<nArgs; i++) printf("%d: %ws\n", i, szArglist[i]); // Free memory allocated for CommandLineToArgvW arguments. LocalFree(szArglist); return(1); } ``` The output of the above code for args `"vim \"""$(wslpath 'D:\my path\my file.txt')""""""` are + For code in list 2 ``` C:\> echo_args_c.exe "vim \"""$(wslpath 'D:\my path\my file.txt')"""""" Command-line arguments: argv[0] echo_args_c.exe argv[1] vim ""$(wslpath 'D:\my path\my file.txt')""" ``` + For code in list 3 ``` C:\Users\CSAR>echo_args.exe "vim \"""$(wslpath 'D:\my path\my file.txt')"""""" 0: echo_args.exe 1: vim ""$(wslpath 2: 'D:\my 3: path\my 4: file.txt')"" ``` If you put two backslashes in the command line, the result will also be ok.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#12768