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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/307198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:46:06  来源:igfitidea点击:

Remove quotes from named environment variables in Windows scripts

windowsbatch-filescriptingenvironment-variablesquotes

提问by Craig Walker

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 barparameter. I then want to launch that URL using the "start" command.

例如:我有一个 URL 前缀,http://example.com?foo=1&bar=并希望通过为bar参数提供一个值来创建一个完整的 URL 。然后我想使用“开始”命令启动该 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?

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

采纳答案by zdan

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.

但是,如果您尝试回显这一点,它会抱怨,因为 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

回答by Craig Walker

echo %myvar:"=%

回声 %myvar:"=%

回答by jimhark

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

回答by Chris

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

回答by Keith

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.htmlfor this method.

此方法的功劳转到http://ss64.com/nt/syntax-esc.html

回答by Christian d'Heureuse

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!

回答by CmdCrazy

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%

回答by Amr Ali

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

This is because the variable contains special shell characters.

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

回答by wimh

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 命令提供一个标题。