View Full Version : Setting an environment variable with an ampersand but no quotes


Rich Pasco
10-27-2005, 02:45 PM
How, from a command line, can I set the environment variable 'user' to
this value:

Rich & Ada

Descriptions of some failed attempts follow:

1. The command line

set user=Rich & Ada

results in the error message

'Ada' is not recognized as an internal or external command,
operable program or batch file.

2. The command line

set user="Rich & Ada"

results in the value

"Rich & Ada"

Notice the unwanted quotation marks.

3. The command line

set user=Rich \& Ada

results in the same error message as #1 above.

Any ideas?

- Rich

David Candy
10-27-2005, 02:55 PM
^ is escape not \
--
--------------------------------------------------------------------------------------------------
Read David defending the concept of violence.
http://margokingston.typepad.com/harry_version_2/2005/10/entering_the_ga.html#more
=================================================
"Rich Pasco" <richp1234@hotmail.com> wrote in message news:%23uJ2lyv2FHA.892@TK2MSFTNGP12.phx.gbl...
> How, from a command line, can I set the environment variable 'user' to
> this value:
>
> Rich & Ada
>
> Descriptions of some failed attempts follow:
>
> 1. The command line
>
> set user=Rich & Ada
>
> results in the error message
>
> 'Ada' is not recognized as an internal or external command,
> operable program or batch file.
>
> 2. The command line
>
> set user="Rich & Ada"
>
> results in the value
>
> "Rich & Ada"
>
> Notice the unwanted quotation marks.
>
> 3. The command line
>
> set user=Rich \& Ada
>
> results in the same error message as #1 above.
>
> Any ideas?
>
> - Rich

foxidrive
10-27-2005, 02:59 PM
On Thu, 27 Oct 2005 09:45:06 -0400, Rich Pasco wrote:

> How, from a command line, can I set the environment variable 'user' to
> this value:
>
> Rich & Ada
>
> Descriptions of some failed attempts follow:
>
> 1. The command line
>
> set user=Rich & Ada
>
> results in the error message
>
> 'Ada' is not recognized as an internal or external command,
> operable program or batch file.
>
> 2. The command line
>
> set user="Rich & Ada"
>
> results in the value
>
> "Rich & Ada"
>
> Notice the unwanted quotation marks.
>
> 3. The command line
>
> set user=Rich \& Ada
>
> results in the same error message as #1 above.
>
> Any ideas?
>
> - Rich

@echo off
set user="Rich & Ada"
for /f "delims=" %%a in (%user%) do echo %%a

David Candy
10-27-2005, 03:01 PM
But what do you intend to do with it. You need to escape to use it anyway.

Type
set a=F ^& M
echo %a%

bingo same problem

set a=F ^^^& M
Puts an escape into the variable's contents.
--
--------------------------------------------------------------------------------------------------
Read David defending the concept of violence.
http://margokingston.typepad.com/harry_version_2/2005/10/entering_the_ga.html#more
=================================================
"David Candy" <.> wrote in message news:u5oWl4v2FHA.3912@TK2MSFTNGP15.phx.gbl...
^ is escape not \
--
--------------------------------------------------------------------------------------------------
Read David defending the concept of violence.
http://margokingston.typepad.com/harry_version_2/2005/10/entering_the_ga.html#more
=================================================
"Rich Pasco" <richp1234@hotmail.com> wrote in message news:%23uJ2lyv2FHA.892@TK2MSFTNGP12.phx.gbl...
> How, from a command line, can I set the environment variable 'user' to
> this value:
>
> Rich & Ada
>
> Descriptions of some failed attempts follow:
>
> 1. The command line
>
> set user=Rich & Ada
>
> results in the error message
>
> 'Ada' is not recognized as an internal or external command,
> operable program or batch file.
>
> 2. The command line
>
> set user="Rich & Ada"
>
> results in the value
>
> "Rich & Ada"
>
> Notice the unwanted quotation marks.
>
> 3. The command line
>
> set user=Rich \& Ada
>
> results in the same error message as #1 above.
>
> Any ideas?
>
> - Rich

William Allen
10-27-2005, 05:11 PM
"Rich Pasco" wrote in message
> How, from a command line, can I set the environment variable 'user' to
> this value:
>
> Rich & Ada
....snip

You need to escape the & operator with ^ (escape operator).

However, if you intend to make use of the USER variable in
a command line, a normal attempt to expand the variable will
fail (owing to the & command separator in the SET variable).

You can work around this by escaping the & in two stages,
by using three ^ characters. This demo batch shows what
goes wrong with one level of escaping, and how to correct
this with two levels of escaping:

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
SETLOCAL

:: SET with escape for one level
SET USER=Buffy ^& Spike

:: Display is OK
ECHO. Display USER contents
SET USER

:: However, expansion may fail as here
ECHO. Try to expand USER contents (this will fail)
ECHO. User=%USER%

:: SET with escape for two levels
SET USER=Buffy ^^^& Spike

:: Display shows one escape level remains
ECHO.
ECHO. Display USER contents
SET USER

:: Expansion will now work correctly
ECHO. Expand USER contents (this will work)
ECHO. User=%User%

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

Screen capture shows operation:

============Screen capture Windows 2000 simulated in Win95
C:\WORK>demo
Display USER contents
USER=Buffy & Spike
Try to expand USER contents (this will fail)
User=Buffy
'Spike' is not recognized as an internal or external command,
operable program or batch file.

Display USER contents
USER=Buffy ^& Spike
Expand USER contents (this will work)
User=Buffy & Spike

C:\WORK>
============End screen capture

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
From email address not checked. Contact us at http://www.allenware.com/

Todd Vargo
10-27-2005, 11:40 PM
"Rich Pasco" <richp1234@hotmail.com> wrote in message
news:#uJ2lyv2FHA.892@TK2MSFTNGP12.phx.gbl...
> How, from a command line, can I set the environment variable 'user' to
> this value:
>
> Rich & Ada
>
> Descriptions of some failed attempts follow:
>
> 1. The command line
>
> set user=Rich & Ada
>
> results in the error message
>
> 'Ada' is not recognized as an internal or external command,
> operable program or batch file.

set "user=Rich & Ada"
echo Hello %user% Somebody

--
Todd Vargo (double "L" to reply by email)

foxidrive
10-28-2005, 03:58 AM
On Thu, 27 Oct 2005 18:40:28 -0400, Todd Vargo wrote:

> "Rich Pasco" <richp1234@hotmail.com> wrote in message
> news:#uJ2lyv2FHA.892@TK2MSFTNGP12.phx.gbl...
>> How, from a command line, can I set the environment variable 'user' to
>> this value:
>>
>> Rich & Ada
>>
>> Descriptions of some failed attempts follow:
>>
>> 1. The command line
>>
>> set user=Rich & Ada
>>
>> results in the error message
>>
>> 'Ada' is not recognized as an internal or external command,
>> operable program or batch file.
>
> set "user=Rich & Ada"
> echo Hello %user% Somebody

Nice try, but no cake. :)

Q:\>set "user=Rich & Ada"

Q:\>echo Hello %user% Somebody
Hello Rich
'Ada' is not recognized as an internal or external command,
operable program or batch file.

Todd Vargo
10-28-2005, 04:49 AM
"foxidrive" <woohoo@gotcha.invalid> wrote in message
news:1vko37ewocu7h.8ldpomje7k1m.dlg@40tude.net...
> On Thu, 27 Oct 2005 18:40:28 -0400, Todd Vargo wrote:
>
> > "Rich Pasco" <richp1234@hotmail.com> wrote in message
> > news:#uJ2lyv2FHA.892@TK2MSFTNGP12.phx.gbl...
> >> How, from a command line, can I set the environment variable 'user' to
> >> this value:
> >>
> >> Rich & Ada
> >>
> >> Descriptions of some failed attempts follow:
> >>
> >> 1. The command line
> >>
> >> set user=Rich & Ada
> >>
> >> results in the error message
> >>
> >> 'Ada' is not recognized as an internal or external command,
> >> operable program or batch file.
> >
> > set "user=Rich & Ada"
> > echo Hello %user% Somebody
>
> Nice try, but no cake. :)
>
> Q:\>set "user=Rich & Ada"
>
> Q:\>echo Hello %user% Somebody
> Hello Rich
> 'Ada' is not recognized as an internal or external command,
> operable program or batch file.

Quite right. I intended but forgot to include the escape character.

E:\>set "user=Rich ^& Ada"

E:\>echo Hello %user% Somebody
Hello Rich & Ada Somebody

--
Todd Vargo (double "L" to reply by email)

Rich Pasco
10-28-2005, 05:10 PM
Thanks, David, for reminding me that ^ is the escape character.

set user=Rich ^& Ada

worked perfectly.

The command script will put quotes around the value when using it:

blat %body% -noh2 -from "%user% <%email%>" -to "%%e" -sf %subject%

so the extra quote is unnecessary.

- Rich


David Candy wrote:

> ^ is escape not \

and:

> But what do you intend to do with it. You need to escape to use it anyway.
>
> Type
> set a=F ^& M
> echo %a%
>
> bingo same problem
>
> set a=F ^^^& M
> Puts an escape into the variable's contents.

David Candy
10-28-2005, 07:44 PM
It's polite to thank the others who put in as much time as me for you.

--
--------------------------------------------------------------------------------------------------
Read David defending the concept of violence.
http://margokingston.typepad.com/harry_version_2/2005/10/entering_the_ga.html#more
=================================================
"Rich Pasco" <richp1234@hotmail.com> wrote in message news:uZgMho92FHA.3000@TK2MSFTNGP12.phx.gbl...
> Thanks, David, for reminding me that ^ is the escape character.
>
> set user=Rich ^& Ada
>
> worked perfectly.
>
> The command script will put quotes around the value when using it:
>
> blat %body% -noh2 -from "%user% <%email%>" -to "%%e" -sf %subject%
>
> so the extra quote is unnecessary.
>
> - Rich
>
>
> David Candy wrote:
>
>> ^ is escape not \
>
> and:
>
>> But what do you intend to do with it. You need to escape to use it anyway.
>>
>> Type
>> set a=F ^& M
>> echo %a%
>>
>> bingo same problem
>>
>> set a=F ^^^& M
>> Puts an escape into the variable's contents.

Rich Pasco
10-29-2005, 10:37 AM
David Candy wrote:

> It's polite to thank the others who put in as much time as me for you.

I believe I did thank you, David, for your suggestion, which was the
first one I read, as soon as I read it, as it solved my problem.

Thanks also to the others whose suggestions I read after I had already
posted thanks to yours.

- Rich

Rich Pasco
10-29-2005, 10:38 AM
foxidrive wrote:

> @echo off
> set user="Rich & Ada"
> for /f "delims=" %%a in (%user%) do echo %%a

Thank you for your time posting this suggestion. However, it does not
set the value of %user% without quotes in it as I requested.

- Rich

Rich Pasco
10-29-2005, 10:40 AM
Thank you for your time in posting this suggestion, which is essentially
what David Candy posted earlier.

Since I put quotes around the value of 'user' when I later use it on a
command line, the double-escape trick is not necessary. It is worth
remembering, though.

- Rich


William Allen wrote:

> "Rich Pasco" wrote in message
>> How, from a command line, can I set the environment variable 'user' to
>> this value:
>>
>> Rich & Ada
> ...snip
>
> You need to escape the & operator with ^ (escape operator).
>
> However, if you intend to make use of the USER variable in
> a command line, a normal attempt to expand the variable will
> fail (owing to the & command separator in the SET variable).
>
> You can work around this by escaping the & in two stages,
> by using three ^ characters. This demo batch shows what
> goes wrong with one level of escaping, and how to correct
> this with two levels of escaping:
>
> Lines that don't begin with two spaces have wrapped accidentally
> ====Begin cut-and-paste (omit this line)
> @ECHO OFF
> SETLOCAL
>
> :: SET with escape for one level
> SET USER=Buffy ^& Spike
>
> :: Display is OK
> ECHO. Display USER contents
> SET USER
>
> :: However, expansion may fail as here
> ECHO. Try to expand USER contents (this will fail)
> ECHO. User=%USER%
>
> :: SET with escape for two levels
> SET USER=Buffy ^^^& Spike
>
> :: Display shows one escape level remains
> ECHO.
> ECHO. Display USER contents
> SET USER
>
> :: Expansion will now work correctly
> ECHO. Expand USER contents (this will work)
> ECHO. User=%User%
>
> ====End cut-and-paste (omit this line)
> Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
> Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects
>
> Screen capture shows operation:
>
> ============Screen capture Windows 2000 simulated in Win95
> C:\WORK>demo
> Display USER contents
> USER=Buffy & Spike
> Try to expand USER contents (this will fail)
> User=Buffy
> 'Spike' is not recognized as an internal or external command,
> operable program or batch file.
>
> Display USER contents
> USER=Buffy ^& Spike
> Expand USER contents (this will work)
> User=Buffy & Spike
>
> C:\WORK>
> ============End screen capture
>
> --
> William Allen
> Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
> Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
> From email address not checked. Contact us at http://www.allenware.com/
>
>

Rich Pasco
10-29-2005, 10:42 AM
Todd Vargo wrote:

> set "user=Rich & Ada"

Neat trick, Todd. It seems to work as well as David Candy's

set user=Rich ^& Ada

and has the same result. Thanks for the suggestion!

- Rich

Rich Pasco
10-29-2005, 10:44 AM
Todd Vargo wrote:

> "foxidrive" <woohoo@gotcha.invalid> wrote in message
> news:1vko37ewocu7h.8ldpomje7k1m.dlg@40tude.net...
>> On Thu, 27 Oct 2005 18:40:28 -0400, Todd Vargo wrote:
>>
>> > "Rich Pasco" <richp1234@hotmail.com> wrote in message
>> > news:#uJ2lyv2FHA.892@TK2MSFTNGP12.phx.gbl...
>> >> How, from a command line, can I set the environment variable 'user' to
>> >> this value:
>> >>
>> >> Rich & Ada
>> >>
>> >> Descriptions of some failed attempts follow:
>> >>
>> >> 1. The command line
>> >>
>> >> set user=Rich & Ada
>> >>
>> >> results in the error message
>> >>
>> >> 'Ada' is not recognized as an internal or external command,
>> >> operable program or batch file.
>> >
>> > set "user=Rich & Ada"
>> > echo Hello %user% Somebody
>>
>> Nice try, but no cake. :)
>>
>> Q:\>set "user=Rich & Ada"
>>
>> Q:\>echo Hello %user% Somebody
>> Hello Rich
>> 'Ada' is not recognized as an internal or external command,
>> operable program or batch file.
>
> Quite right. I intended but forgot to include the escape character.
>
> E:\>set "user=Rich ^& Ada"
>
> E:\>echo Hello %user% Somebody
> Hello Rich & Ada Somebody
>
> --
> Todd Vargo (double "L" to reply by email)

Actually, I don't want to embed the escape character ^ into the
environment string, because I put quotes around it when I later use it.

- Rich