Remove quotes from named environment variables in Windows scripts

WindowsBatch FileScriptingEnvironment VariablesQuotes

Windows Problem Overview


I want to store a URL prefix in an Windows environment variable. The ampersands in the query string makes this troublesome though.

For example: I have a URL prefix of http://example.com?foo=1&bar= and want to create a full URL by providing a value for the bar parameter. I then want to launch that URL using the "start" command.

Adding quotes around the value for the SET operation is easy enough:

set myvar="http://example.com?foo=1&bar="

Windows includes the quotes in the actual value though (thanks Windows!):

echo %myvar%
"http://example.com?foo=1&bar=true"

I know that I can strip quotes away from batch file arguments by using tilde:

echo %~1

However, I can't seem to do it to named variables:

echo %~myvar%
%~myvar%

What's the syntax for accomplishing this?

Windows Solutions


Solution 1 - Windows

echo %myvar:"=%

Solution 2 - Windows

This is not a limitation of the environment variable, but rather the command shell.

Enclose the entire assignment in quotes:

set "myvar=http://example.com?foo=1&bar="

Though if you try to echo this, it will complain as the shell will see a break in there.

You can echo it by enclosing the var name in quotes:

echo "%myvar%"

Or better, just use the set command to view the contents:

set myvar

Solution 3 - Windows

While there are several good answers already, another way to remove quotes is to use a simple subroutine:

:unquote
  set %1=%~2
  goto :EOF

Here's a complete usage example:

@echo off
setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS

set words="Two words"
call :unquote words %words%
echo %words%

set quoted="Now is the time"
call :unquote unquoted %quoted%
echo %unquoted%

set word=NoQuoteTest
call :unquote word %word%
echo %word%

goto :EOF

:unquote
  set %1=%~2
  goto :EOF

Solution 4 - Windows

This works

for %a in (%myvar%) do set myvar=%~a

I would also use this if I wanted to print a variable that contained and ampersand without the quotes.

for %a in ("fish & chips") do echo %~a

Solution 5 - Windows

To remove only beginning and ending quotes from a variable:

SET myvar=###%myvar%###
SET myvar=%myvar:"###=%
SET myvar=%myvar:###"=%
SET myvar=%myvar:###=%

This assumes you don't have a ###" or "### inside your value, and does not work if the variable is NULL.

Credit goes to http://ss64.com/nt/syntax-esc.html for this method.

Solution 6 - Windows

Use delayed environment variable expansion and use !var:~1,-1! to remove the quotes:

@echo off
setlocal enabledelayedexpansion
set myvar="http://example.com?foo=1&bar="
set myvarWithoutQuotes=!myvar:~1,-1!
echo !myvarWithoutQuotes!

Solution 7 - Windows

Use multiple variables to do it:

set myvar="http://example.com?foo=1&bar="

set bar=true

set launch=%testvar:,-1%%bar%"

start iexplore %launch%

Solution 8 - Windows

@echo off
set "myvar=http://example.com?foo=1&bar="
setlocal EnableDelayedExpansion
echo !myvar!

This is because the variable contains special shell characters.

Solution 9 - Windows

I think this should do it:

for /f "tokens=*" %i in (%myvar%) do set %myvar%=%~i

But you do not need this,

set myvar="http://example.com?foo=1&bar="
start "" %myvar%

Will work too, you just need to supply a title to the start command.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCraig WalkerView Question on Stackoverflow
Solution 1 - WindowsbenView Answer on Stackoverflow
Solution 2 - WindowszdanView Answer on Stackoverflow
Solution 3 - WindowsjimharkView Answer on Stackoverflow
Solution 4 - WindowsChrisView Answer on Stackoverflow
Solution 5 - WindowsKeithView Answer on Stackoverflow
Solution 6 - WindowsChristian d'HeureuseView Answer on Stackoverflow
Solution 7 - WindowsCmdCrazyView Answer on Stackoverflow
Solution 8 - WindowsAmr AliView Answer on Stackoverflow
Solution 9 - WindowswimhView Answer on Stackoverflow