Setting an environment variable with an ampersand but no quotes

  • Thread starter Thread starter Rich Pasco
  • Start date Start date
R

Rich Pasco

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
 
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
 
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.
--
 
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
 
Rich Pasco said:
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
 
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.
 
foxidrive said:
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
 
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 said:
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
 
foxidrive said:
@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
 
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
 
Todd said:
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
 
Todd said:
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

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
 
Back
Top