6

I need to pass filename as input to a powershell command which includes spaces. Hence I am hoping to include double quotes.

However, Matlab "eats" all double quotes in the input of system() when passing arguments to powershell.

For example, note how the examples below all the the same output.

>> system('powershell.exe echo a c','-echo')
a 
c 
ans =
     0
>> system('powershell.exe echo "a c"','-echo')
a 
c 
ans =
     0
>> system('powershell.exe echo ""a c""','-echo')
a 
c 
ans =
     0
>> system(['powershell.exe echo ',char(34),'a c',char(34)],'-echo')
a 
c 
ans =
     0

The actual output for echo "a c" in powershell is a c in a single line. The change of line only occurs without double quotes.

Just for experiment, I also tried ""a c"" and the expected input is the same as change line, a, change line, c. With the return, it seems that any and all double quotes are "eaten" alive by Matlab.

How do I bring the double quotes back when using system()?

Argyll
  • 8,591
  • 4
  • 25
  • 46
  • Interesting! Could this be related to the invocation of powershell.exe? What happens if you type `powershell.exe echo "a c"` at a cmd.exe prompt? – Cris Luengo Aug 10 '22 at 13:30
  • @CrisLuengo: The double quotes are tragically eaten as well! – Argyll Aug 10 '22 at 13:32
  • Maybe they need escaping? `powershell.exe echo \"a c\"`? I don’t have Windows, and have no idea of how its command line works… :( – Cris Luengo Aug 10 '22 at 13:36
  • @CrisLuengo: Yes, that worked. Thanks for the tip! Would you like to write an answer? It seems calling 'system('powershell.exe ...') in Matlab is the same as calling `powershell.exe ...` in Windows cmd. Somewhere, `"` is filtered from arguments. – Argyll Aug 10 '22 at 13:43
  • I don’t have Windows, so find it awkward to write this up as an answer. Could you please write the answer? The docs to `system` say a new `cmd.exe` prompt is started for each invocation of `system`, so it makes sense for the two to be the same. – Cris Luengo Aug 10 '22 at 13:48

1 Answers1

4

It is PowerShell that is eating your double quotes:

  • You're passing a command (piece of PowerShell code) to the PowerShell CLI, via the -Command (-c) parameter (which is positionally implied in your case).

  • " characters that should be considered part of the command must be escaped as \" (sic)

    • The reason that unescaped " don't work is that PowerShell considers them to have syntactic function on the command line only - they are simply stripped after all arguments have been parsed; the resulting tokens are then joined with spaces, and the resulting string finally interpreted as PowerShell code.
  • While using just \" in your command would fix the problem, it is advisable to also enclose the entire command being passed in "...", because that prevents potentially unwanted whitespace normalization.

system('powershell.exe " echo \"a c\" "', '-echo')

Caveat:

  • Since MatLab's system() function executes the given command line via cmd.exe (which is inefficient in your case, since you don't need shell functionality), use of \" can break the invocation, due to how cmd.exe's parses command lines.

  • To avoid edge cases when cmd.exe is involved, enclose the overall command in "...." and escape pass-through " as follows:

    • Use "^"" (sic) when calling powershell.exe (the Windows PowerShell CLI)
    • Use "" when calling pwsh.exe (the PowerShell (Core) 7+ CLI).
    • See this answer for more information.
mklement0
  • 382,024
  • 64
  • 607
  • 775