从Windows脚本中的命名环境变量中删除引号

时间:2022-06-18 11:34:43

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

我想在Windows环境变量中存储URL前缀。查询字符串中的&符号使这很麻烦。

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.

例如:我的网址前缀为http://example.com?foo=1&bar=,并希望通过为bar参数提供值来创建完整的网址。然后我想使用“start”命令启动该URL。

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

在SET操作的值周围添加引号很容易:

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

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

Windows虽然包含实际值中的引号(感谢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?

完成此操作的语法是什么?

9 个解决方案

#1


28  

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

这不是环境变量的限制,而是命令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.

虽然如果你试图回应这个,它会抱怨,因为shell会在那里看到一个突破。

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

您可以通过将var名称括在引号中来回显它:

echo "%myvar%"

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

或者更好,只需使用set命令查看内容:

set myvar

#2


36  

echo %myvar:"=%

#3


9  

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

#4


7  

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

#5


2  

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

使用延迟环境变量扩展并使用!var:~1,-1!删除引号:

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

#6


2  

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.

假设您的值中没有###“或”###,并且如果变量为NULL则不起作用。

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

对于此方法,请转至http://ss64.com/nt/syntax-esc.html。

#7


1  

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%

#8


0  

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

This is because the variable contains special shell characters.

这是因为变量包含特殊的shell字符。

#9


-1  

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.

也会工作,你只需要为start命令提供一个标题。

#1


28  

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

这不是环境变量的限制,而是命令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.

虽然如果你试图回应这个,它会抱怨,因为shell会在那里看到一个突破。

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

您可以通过将var名称括在引号中来回显它:

echo "%myvar%"

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

或者更好,只需使用set命令查看内容:

set myvar

#2


36  

echo %myvar:"=%

#3


9  

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

#4


7  

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

#5


2  

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

使用延迟环境变量扩展并使用!var:~1,-1!删除引号:

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

#6


2  

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.

假设您的值中没有###“或”###,并且如果变量为NULL则不起作用。

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

对于此方法,请转至http://ss64.com/nt/syntax-esc.html。

#7


1  

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%

#8


0  

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

This is because the variable contains special shell characters.

这是因为变量包含特殊的shell字符。

#9


-1  

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.

也会工作,你只需要为start命令提供一个标题。