删除 Windows 脚本中命名环境变量的引号
我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
回显%myvar:“=%
echo %myvar:"=%
这不是环境变量的限制,而是命令 shell 的限制。
将整个作业用引号括起来:
尽管如果您尝试回显这一点,它会抱怨,因为 shell 会在其中看到中断。
您可以通过将 var 名称括在引号中来回显它:
或者更好,只需使用 set 命令来查看内容:
This is not a limitation of the environment variable, but rather the command shell.
Enclose the entire assignment in quotes:
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:
Or better, just use the set command to view the contents:
虽然已经有几个很好的答案,但删除引号的另一种方法是使用一个简单的子例程:
这是一个完整的用法示例:
While there are several good answers already, another way to remove quotes is to use a simple subroutine:
Here's a complete usage example:
。
如果我想打印一个包含和符号但不带引号的变量,我也会使用它
This works
I would also use this if I wanted to print a variable that contained and ampersand without the quotes.
要仅从变量中删除开头和结尾引号:
这假设您的值中没有 ###" 或 "###,并且如果变量为 NULL,则不起作用。
信用转到 http://ss64.com/nt/syntax-esc.html这个方法。
To remove only beginning and ending quotes from a variable:
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.
使用延迟环境变量扩展并使用 !var:~1,-1! 删除引号:
Use delayed environment variable expansion and use !var:~1,-1! to remove the quotes:
使用多个变量来做到这一点:
Use multiple variables to do it:
这是因为该变量包含特殊的 shell 字符。
This is because the variable contains special shell characters.
我认为这应该可以做到:
但是你不需要这个,
也可以工作,你只需要为启动命令提供一个标题。
I think this should do it:
But you do not need this,
Will work too, you just need to supply a title to the start command.