Dr. Hans-Peter Störr
Dr. Hans-Peter Störr

Reputation: 26006

passing '=' in arguments to windows batch files

I ran into trouble when passing an argument that ends with an = sign to batch files. Consider this (> being the Windows XP prompt):

> type c.bat
echo %1

> c.bat bla=
bla

> c.bat "bla="
"bla="

Why is the = in bla= swallowed? And how am I supposed to pass an = sign in an argument?

Upvotes: 3

Views: 1764

Answers (4)

jeb
jeb

Reputation: 82400

You can use

your.bat "bla="

------ your.bat ----
echo %~1

or also

your.bat bla=

------ your.bat ----
echo %*

The best solution is case dependent.

A description is at

call /?

Normally it's better to enclose problematic parameters into quotes and remove the quotes later with the %~ modifier.
The set "param1=%~1" uses the fact, that %~1 will remove enclosing quotes when they exists.
And the quotes around "param1=%~1" ensure that special charaters will not produce errors.

call :myFunc "<Hard>=&|"
exit /b

:myFunc
setlocal EnableDelayedExpansion
set "param1=%~1"
echo(!param1!
exit /b

But with quotes inside the parameter, problems can still occours.

call :myFunc "&"^&""

Upvotes: 1

Foo Bah
Foo Bah

Reputation: 26281

Read the doc:

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx?mfr=true

"The following special characters require quotation marks: & < > [ ] { } ^ = ; ! ' + , ` ~ [white space]"

Upvotes: 2

Tedd Hansen
Tedd Hansen

Reputation: 12326

It is a known problem: http://support.microsoft.com/kb/35938

You can bypass it like this:

SET ARG1=bla= && your.bat && SET ARG1=

and your.bat:

@ECHO OFF
ECHO %ARG1%

Alternatively you can pass it as a quoted parameter (using "" around it):

your.bat "bla="

and then use it in the batch file as follows:

echo %~1

The ~ there in this case just removes the quotes.

Upvotes: 7

Andy Morris
Andy Morris

Reputation: 3503

will this do it?

If tem.cmd contains

@echo off
set param=%1
set param=%param:"=%
echo %param%

then

c:\> tem.cmd "hi="

gives

hi=

Upvotes: 0

Related Questions