如何将反斜杠参数传递给脚本? [英] How to pass backslashes argument to a script?
问题描述
我只想在文本文件中附加一个用户输入参数.我正在使用以下命令:
I just want to append an user input argument in a text file. I'm using the following command:
echo "$2" >> "./db.txt"
希望用户
$ 2
设置路径,例如: D:\ Projects \ MyProject
.它写入文件,但没有反斜杠.结果是:
$2
is expected to user to set a path like: D:\Projects\MyProject
. It writes to the file, but with no backslashes. The result is:
D:ProjectsMyProject
D:ProjectsMyProject
我在任何地方都找不到解决方法.我尝试使用 -e
或 -E
参数(从这里获取),但在这种情况下似乎没有什么区别.
I can't find anywhere how to fix that. I've tried using -e
or -E
params(taken from here) but it doesn't seems to make difference in this case.
推荐答案
您必须在shell中转义反斜杠: \\
,或将字符串放在引号中.
You have to escape backslashes in the shell: \\
, or put the string in quotes.
出于调试目的,请使用 set -x
.
For debugging purposes use set -x
.
这篇关于如何将反斜杠参数传递给脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!