Why does PowerShell (with Perl) remove double quotes in a simple print request?

I have been a Linux user for a long time, but new to Windows and PowerShell. I just installed Windows7 and Strawberry Perl 5 for the first time. Now I want to do simple command line printing using Windows PowerShell.

It seems that Perl is installed correctly:

  PS C: \ Users \ Me> perl -v 
 This is perl, v5.10.0 built for MSWin32-x86-multi-thread Copyright
 1987-2007, Larry Wall 
 ...

And the command line works:

  PS C: \ Users \ Me> perl -e 'die'
 Died at -e line 1.

 PS C: \ Users \ Me> echo 'print "Hello, World \ n"' |  perl
 Hello world

But when I try to do this on my own, it prints a warning with the file manager:

  PS C: \ Users \ Me> perl -e 'print "Hello, World \ n"'
 No comma allowed after filehandle at -e line 1.

So it looks like it removes double quotes.

  PS C: \ Users \ Me> perl -e 'print \ "Hello, World \ n \"'
 Hello world

It works, but its ugly! Try again:

  PS C: \ Users \ Me> perl -e 'print qq {Hello, World \ n}'
 Hello world

I like it more, but I'm confused.

Why does PowerShell avoid double quotes in single quotes? Any PowerShell users?

+6
perl powershell
source share
3 answers

I think there are a couple of problems here. First, quotes are symbolic in both powershell and perl. Quotations and escaping work differently in powershell than in UNIX shell. See man about_quoting in powershell.

The second problem is that the perl command line behaves differently during the Windows period. Any double quote inside the command line that you want to pass perl must be escaped in perl terms as \ ". This does not apply to powershell. This is how perl works on Windows. Cmd.exe.

These versions should work:

PS> & perl -e "print \`"hello, world\n\`"" hello, world PS> $s = "print \`"hello, world\n\`"" PS> echo $s print \"hello, world\n\" PS> & perl -e $s hello, world 

You can do the same with less acceleration using a single quote.

 PS> & perl -e 'print \"hello, world\n\"' hello, world PS> $s = 'print \"hello, world\n\"' PS> echo $s print \"hello, world\n\" PS> & perl -e $s hello, world 

You can also put a newline in powershell instead of passing the escape sequence of the newline to perl for parsing.

 PS> & perl -e "print \`"hello, world`n\`"" hello, world PS> $s = "print \`"hello, world`n\`"" PS> echo $s print \"hello, world \" PS> & perl -e $s hello, world 
+3
source share

Interesting. It seems that when you leave the powershell world, it will convert a single quote to a double quote, which will be correctly interpreted by the Windows shell. (Or something like that). To demonstrate with python (which seems to have the same problem):

 PS C:\> python -c 'print "Hi from python"' Traceback (most recent call last): File "<string>", line 1, in <module> NameError: name 'Hi' is not defined 

which is the same error i get if i tried to do something like this from windows shell:

 C:\>python -c "print "Hi from python"" Traceback (most recent call last): File "<string>", line 1, in <module> NameError: name 'Hi' is not defined 

As if powershell does this when executing the command:

 PS C:\> $s = 'python -c "print "Hi from python""' PS C:\> cmd /c $s Traceback (most recent call last): File "<string>", line 1, in <module> NameError: name 'Hi' is not defined 
+1
source share

What happens if you run the "wrong" command in cmd.exe instead of PowerShell? I ask because the escape sequence that works ( \" ) is Perl; in PowerShell it will be` `` `.

0
source share

All Articles