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

发布于 2024-07-09 02:35:52 字数 594 浏览 4 评论 0原文

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

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

在 SET 操作的值周围添加引号非常简单:

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

Windows 在实际值中包含引号(感谢 Windows!):

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

我知道我可以使用波形符从批处理文件参数中删除引号:

echo %~1

但是,我不能似乎对命名变量执行此操作:

echo %~myvar%
%~myvar%

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

逐鹿 2024-07-16 02:35:53

回显%myvar:“=%

echo %myvar:"=%

魄砕の薆 2024-07-16 02:35:53

这不是环境变量的限制,而是命令 shell 的限制。

将整个作业用引号括起来:

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

尽管如果您尝试回显这一点,它会抱怨,因为 shell 会在其中看到中断。

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

echo "%myvar%"

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

set myvar

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
她比我温柔 2024-07-16 02:35:53

虽然已经有几个很好的答案,但删除引号的另一种方法是使用一个简单的子例程:

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

这是一个完整的用法示例:

@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

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
梦年海沫深 2024-07-16 02:35:53

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

如果我想打印一个包含和符号但不带引号的变量,我也会使用它

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

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
芸娘子的小脾气 2024-07-16 02:35:53

要仅从变量中删除开头和结尾引号:

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

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

信用转到 http://ss64.com/nt/syntax-esc.html这个方法。

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.

唱一曲作罢 2024-07-16 02:35:53

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

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

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!
怕倦 2024-07-16 02:35:53

使用多个变量来做到这一点:

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

set bar=true

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

start iexplore %launch%

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%
尘曦 2024-07-16 02:35:53
@echo off
set "myvar=http://example.com?foo=1&bar="
setlocal EnableDelayedExpansion
echo !myvar!

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

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

This is because the variable contains special shell characters.

对岸观火 2024-07-16 02:35:53

我认为这应该可以做到:

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

但是你不需要这个,

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

也可以工作,你只需要为启动命令提供一个标题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文