Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Mix single and double quotes

Reply
 
Old 10-21-2006   #1 (permalink)
Jean


 
 

Mix single and double quotes

I don't know if this is a bug or not (yet?) (full) supported.

"Mix" here obviously means "Alternate".

It doesn't work at all in expression mode :

'$X'"Y"'$X'
or
"Y"'$X'"Y"

raise an error.

It "half-works" in command mode :
# <sq>$X</sq><dq>Y</dq><sq>$X</sq>
write-host '$X'"Y"'$X'

returns :

$X Y $X

or

# <dq>Y</dq><sq>$X</sq><dq>Y</dq>
write-host "Y"'$X'"Y"

returns :

Y $X Y

As you see it "half-works" as spaces are added and we should expect :
$XY$X and Y$XY as returns.

Regards,

--
Jean - JMST
Belgium



My System SpecsSystem Spec
Old 10-21-2006   #2 (permalink)
klumsy@xtra.co.nz


 
 

Re: Mix single and double quotes

try this syntax

@'
" can't we see '' ' " " that we are mixxing something
'@


betwene the @' and the '@ its totally literal, no matter how many
double or single quotes there are, or even ` escape codes.. they don't
get escaped.

one thing to no is its a tedious syntax
you have to have them on different lines, and even an extra space after
the @' will make it bomb.


Jean wrote:
> I don't know if this is a bug or not (yet?) (full) supported.
>
> "Mix" here obviously means "Alternate".
>
> It doesn't work at all in expression mode :
>
> '$X'"Y"'$X'
> or
> "Y"'$X'"Y"
>
> raise an error.
>
> It "half-works" in command mode :
> # <sq>$X</sq><dq>Y</dq><sq>$X</sq>
> write-host '$X'"Y"'$X'
>
> returns :
>
> $X Y $X
>
> or
>
> # <dq>Y</dq><sq>$X</sq><dq>Y</dq>
> write-host "Y"'$X'"Y"
>
> returns :
>
> Y $X Y
>
> As you see it "half-works" as spaces are added and we should expect :
> $XY$X and Y$XY as returns.
>
> Regards,
>
> --
> Jean - JMST
> Belgium


My System SpecsSystem Spec
Old 10-21-2006   #3 (permalink)
dreeschkind


 
 

RE: Mix single and double quotes

Some more workarounds:

a) write-host "Y"'$X'"Y" -s ""

b) write-host ("Y"+'$X'+"Y")

c) $ofs="";[string]("Y",'$X',"Y")

d) ("Y"+'$X'+"Y")

--
greetings
dreeschkind

"Jean" wrote:

> I don't know if this is a bug or not (yet?) (full) supported.
>
> "Mix" here obviously means "Alternate".
>
> It doesn't work at all in expression mode :
>
> '$X'"Y"'$X'
> or
> "Y"'$X'"Y"
>
> raise an error.
>
> It "half-works" in command mode :
> # <sq>$X</sq><dq>Y</dq><sq>$X</sq>
> write-host '$X'"Y"'$X'
>
> returns :
>
> $X Y $X
>
> or
>
> # <dq>Y</dq><sq>$X</sq><dq>Y</dq>
> write-host "Y"'$X'"Y"
>
> returns :
>
> Y $X Y
>
> As you see it "half-works" as spaces are added and we should expect :
> $XY$X and Y$XY as returns.
>
> Regards,
>
> --
> Jean - JMST
> Belgium
>
>
>

My System SpecsSystem Spec
Old 10-21-2006   #4 (permalink)
Jean


 
 

Re: Mix single and double quotes

> try this syntax
>
> @'
> " can't we see '' ' " " that we are mixxing something
> '@
>


Thank you for your answer and the trick.

But I wanted a "mix" preserving "expand" double quote contents and
"unexpand" single quote contents.

ie to be able to do :

$test="Z"
write-host '$test'"$test"'$test'
giving
$testZ$test

Regards,

--
Jean - JMST
Belgium


My System SpecsSystem Spec
Old 10-21-2006   #5 (permalink)
Jean


 
 

Re: Mix single and double quotes

> Some more workarounds:
>
> a) write-host "Y"'$X'"Y" -s ""
>
> b) write-host ("Y"+'$X'+"Y")
>
> c) $ofs="";[string]("Y",'$X',"Y")
>
> d) ("Y"+'$X'+"Y")
>


Thank you.
b) and d) are my favorites :-)

As in my project I "need" <sq></sq><dq></dq> I use `b to start the
double quote string to get rid the extra space, ie :

write-host '$SINGLE'"`bDOUBLE"

BTW, is it a way to know if a string variable is single or double
quoted ?

Regards,

--
Jean - JMST
Belgium


My System SpecsSystem Spec
Old 10-21-2006   #6 (permalink)
dreeschkind


 
 

Re: Mix single and double quotes

"Jean" wrote:

> BTW, is it a way to know if a string variable is single or double
> quoted ?


AFAIK this is not directly possible. Everything in double quotes will be
expanded as soon as it is being assigned to a string variable. However, using
non printable characters, you can get a hint on how the string has been
created:

>$a = "`a123"
>$a.length

4

>$a = '`a123'
>$a.length

5

--
greetings
dreeschkind
My System SpecsSystem Spec
Old 10-22-2006   #7 (permalink)
Jacques Barathon [MS]


 
 

Re: Mix single and double quotes

"Jean" <repondre@groupe.svp> wrote in message
news:mn.b03f7d6a30ab6da4.56820@windows...
> But I wanted a "mix" preserving "expand" double quote contents and
> "unexpand" single quote contents.
>
> ie to be able to do :
>
> $test="Z"
> write-host '$test'"$test"'$test'
> giving
> $testZ$test


In an expandable string you can prevent expansion by escaping the $ sign:

PS> write-host "`$test$test`$test"
$testZ$test
PS> "`$test$test`$test"
$testZ$test

A bit more fun:

PS> $a='`$test$test`$test'
PS> $a
`$test$test`$test
PS> $b='"'+$a+'"'
PS> $b
"`$test$test`$test"
PS> invoke-expression $b
$testZ$test

Or a very silly way to get the same result:

PS> "$"
$
PS> "$$test$test$$test"
$testZ$test

Jacques

My System SpecsSystem Spec
Old 10-22-2006   #8 (permalink)
Jean


 
 

Re: Mix single and double quotes

> In an expandable string you can prevent expansion by escaping the $ sign:

Thank you, this complete well the topic.

Don't know yet which way I'll choice from answers.

To come back to my questioning I conclude alternate single and double
quotes in command mode as in : write-host "Y"'$X'"Y" isn't really
supported/intended and so could be breaked in future PS's releases.

Regards,

--
Jean - JMST
Belgium


My System SpecsSystem Spec
Old 10-22-2006   #9 (permalink)
Jean


 
 

Re: Mix single and double quotes

> AFAIK this is not directly possible.

In fact, to be possible, imho this would imply a [unexpandedstring]
type or an "unexpanded" property in [string] type.

Regards,

--
Jean - JMST
Belgium


My System SpecsSystem Spec
Old 10-23-2006   #10 (permalink)
Alex K. Angelopoulos [MVP]


 
 

Re: Mix single and double quotes


"Jean" <repondre@groupe.svp> wrote in message
news:mn.b5967d6a27089c2e.56820@windows...

> To come back to my questioning I conclude alternate single and double
> quotes in command mode as in : write-host "Y"'$X'"Y" isn't really
> supported/intended and so could be breaked in future PS's releases.


I hadn't thought about that. :|

I already tend to just use simple escaping when needed, inserting a ` for
any special character in a string. One thing that is particularly handy
about this approach is that you can even escape linear whitespace with `.



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Re: Remove double quotes from a string .NET General
shortcuts targetpath has double quotes and won't work? PowerShell
Need info on how to escape double quotes PowerShell
Need info on how to escape double quotes PowerShell
here-string with single and double quotes PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46