Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Windows 7 Forum Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Using $ signs in strings

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 05-21-2008   #1 (permalink)
greatbarrier86
Guest


 

Using $ signs in strings

Hi,

I recently tried to put a $ sign into a string for a variable but it kept
coming back as cutting off everything after the $ though it thought the
string was the combination of a string and another variable. If i have a
string which contains a $ sign, how do i write it to allow it to be
recognized as just part of the string, and not a seperate variable.

Here is how i am doing it now. This is the only way i could find to get it
to work.

$Variable = "12345abc" + "$" + "12345abc"

There has to be an easier way than that.

My System SpecsSystem Spec
Old 05-21-2008   #2 (permalink)
Marco Shaw [MVP]
Guest


 

Re: Using $ signs in strings

greatbarrier86 wrote:
Quote:

> Hi,
>
> I recently tried to put a $ sign into a string for a variable but it kept
> coming back as cutting off everything after the $ though it thought the
> string was the combination of a string and another variable. If i have a
> string which contains a $ sign, how do i write it to allow it to be
> recognized as just part of the string, and not a seperate variable.
>
> Here is how i am doing it now. This is the only way i could find to get it
> to work.
>
> $Variable = "12345abc" + "$" + "12345abc"
>
> There has to be an easier way than that.
Use a backtick to escape the $:

PS> $variable="123`$abc"
PS> $variable
123$abc

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Old 05-21-2008   #3 (permalink)
greatbarrier86
Guest


 

Re: Using $ signs in strings

Yep! that worked. Can you explain what you mean by "escape the $." I got the
meaning, but why "escape"?

"Marco Shaw [MVP]" wrote:
Quote:

> greatbarrier86 wrote:
Quote:

> > Hi,
> >
> > I recently tried to put a $ sign into a string for a variable but it kept
> > coming back as cutting off everything after the $ though it thought the
> > string was the combination of a string and another variable. If i have a
> > string which contains a $ sign, how do i write it to allow it to be
> > recognized as just part of the string, and not a seperate variable.
> >
> > Here is how i am doing it now. This is the only way i could find to get it
> > to work.
> >
> > $Variable = "12345abc" + "$" + "12345abc"
> >
> > There has to be an easier way than that.
>
> Use a backtick to escape the $:
>
> PS> $variable="123`$abc"
> PS> $variable
> 123$abc
>
> --
> Microsoft MVP - Windows PowerShell
> http://www.microsoft.com/mvp
>
> PowerGadgets MVP
> http://www.powergadgets.com/mvp
>
> Blog:
> http://marcoshaw.blogspot.com
>
My System SpecsSystem Spec
Old 05-21-2008   #4 (permalink)
greatbarrier86
Guest


 

Re: Using $ signs in strings

Thanks to both of you. Shay, i like your suggestion but what bothers me is
that to someone else just starting out, it may seem strange that one of the
strings in single while the rest are in double. Personally, it wouldnt
bother me either way. What do you think?

"Shay Levi" wrote:
Quote:

>
> Another option is to wrap the string in single quotes:
>
> PS > $Variable = '12345$abc'
> PS > $Variable
> 12345$abc
>
>
>
> ---
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
>
Quote:

> > Hi,
> >
> > I recently tried to put a $ sign into a string for a variable but it
> > kept coming back as cutting off everything after the $ though it
> > thought the string was the combination of a string and another
> > variable. If i have a string which contains a $ sign, how do i write
> > it to allow it to be recognized as just part of the string, and not a
> > seperate variable.
> >
> > Here is how i am doing it now. This is the only way i could find to
> > get it to work.
> >
> > $Variable = "12345abc" + "$" + "12345abc"
> >
> > There has to be an easier way than that.
> >
>
>
>
My System SpecsSystem Spec
Old 05-21-2008   #5 (permalink)
Marco Shaw [MVP]
Guest


 

Re: Using $ signs in strings

greatbarrier86 wrote:
Quote:

> Yep! that worked. Can you explain what you mean by "escape the $." I got the
> meaning, but why "escape"?
PSH> help about_escape

Interpret the following character differently...
My System SpecsSystem Spec
Old 05-21-2008   #6 (permalink)
Shay Levi
Guest


 

Re: Using $ signs in strings


Another option is to wrap the string in single quotes:

PS > $Variable = '12345$abc'
PS > $Variable
12345$abc



---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> Hi,
>
> I recently tried to put a $ sign into a string for a variable but it
> kept coming back as cutting off everything after the $ though it
> thought the string was the combination of a string and another
> variable. If i have a string which contains a $ sign, how do i write
> it to allow it to be recognized as just part of the string, and not a
> seperate variable.
>
> Here is how i am doing it now. This is the only way i could find to
> get it to work.
>
> $Variable = "12345abc" + "$" + "12345abc"
>
> There has to be an easier way than that.
>

My System SpecsSystem Spec
Old 05-21-2008   #7 (permalink)
greatbarrier86
Guest


 

Re: Using $ signs in strings

Thanks!

"Marco Shaw [MVP]" wrote:
Quote:

> greatbarrier86 wrote:
Quote:

> > Yep! that worked. Can you explain what you mean by "escape the $." I got the
> > meaning, but why "escape"?
>
> PSH> help about_escape
>
> Interpret the following character differently...
>
My System SpecsSystem Spec
Old 05-21-2008   #8 (permalink)
greatbarrier86
Guest


 

Re: Using $ signs in strings

So you suggest not using the escape char?

"Shay Levi" wrote:
Quote:

>
> It may confuse at first but it has a good reason, type this in your console
> to get the explanation:
>
> PS > help about_quoting_rules
>
>
> ---
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
>
Quote:

> > Thanks to both of you. Shay, i like your suggestion but what bothers
> > me is that to someone else just starting out, it may seem strange that
> > one of the strings in single while the rest are in double.
> > Personally, it wouldnt bother me either way. What do you think?
> >
> > "Shay Levi" wrote:
> >
Quote:

> >> Another option is to wrap the string in single quotes:
> >>
> >> PS > $Variable = '12345$abc'
> >> PS > $Variable
> >> 12345$abc
> >> ---
> >> Shay Levi
> >> $cript Fanatic
> >> http://scriptolog.blogspot.com
> >>> Hi,
> >>>
> >>> I recently tried to put a $ sign into a string for a variable but it
> >>> kept coming back as cutting off everything after the $ though it
> >>> thought the string was the combination of a string and another
> >>> variable. If i have a string which contains a $ sign, how do i write
> >>> it to allow it to be recognized as just part of the string, and not
> >>> a seperate variable.
> >>>
> >>> Here is how i am doing it now. This is the only way i could find to
> >>> get it to work.
> >>>
> >>> $Variable = "12345abc" + "$" + "12345abc"
> >>>
> >>> There has to be an easier way than that.
> >>>
>
>
>
My System SpecsSystem Spec
Old 05-21-2008   #9 (permalink)
Marco Shaw [MVP]
Guest


 

Re: Using $ signs in strings

greatbarrier86 wrote:
Quote:

> So you suggest not using the escape char?
Depends... Using single vs double depends on what you're trying to achieve.

PSH>"abc`$123"
abc$123
PSH>$foo="@#"
PSH>"abc`$123$foo"
abc$123@#
PSH>'abc`$123$foo'
abc`$123$foo <--Definitely not the same outcome
PSH>
My System SpecsSystem Spec
Old 05-21-2008   #10 (permalink)
Shay Levi
Guest


 

Re: Using $ signs in strings


It may confuse at first but it has a good reason, type this in your console
to get the explanation:

PS > help about_quoting_rules


---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> Thanks to both of you. Shay, i like your suggestion but what bothers
> me is that to someone else just starting out, it may seem strange that
> one of the strings in single while the rest are in double.
> Personally, it wouldnt bother me either way. What do you think?
>
> "Shay Levi" wrote:
>
Quote:

>> Another option is to wrap the string in single quotes:
>>
>> PS > $Variable = '12345$abc'
>> PS > $Variable
>> 12345$abc
>> ---
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
Quote:

>>> Hi,
>>>
>>> I recently tried to put a $ sign into a string for a variable but it
>>> kept coming back as cutting off everything after the $ though it
>>> thought the string was the combination of a string and another
>>> variable. If i have a string which contains a $ sign, how do i write
>>> it to allow it to be recognized as just part of the string, and not
>>> a seperate variable.
>>>
>>> Here is how i am doing it now. This is the only way i could find to
>>> get it to work.
>>>
>>> $Variable = "12345abc" + "$" + "12345abc"
>>>
>>> There has to be an easier way than that.
>>>

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
WLM signs me out when I try and send a message...PLEASE HELP!! klee82 Live Messenger 1 1 Week Ago 05:33 PM
+ signs next to in box messages Steve Vista mail 1 08-21-2008 07:57 AM
Plus signs in file names granovet Live Folder Share 0 07-21-2008 05:32 PM
FS Signs in all libraries are greyed out johnc Live Folder Share 1 06-16-2008 07:47 PM
Know about the signs on DVD discs Jazz2008 Vista General 2 05-21-2008 08:18 AM


Vistax64.com 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 2005-2008

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 47 48 49 50 51