bash 如何在bash脚本中转义字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5220325/
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
How to escape strings in bash script
提问by mit
I am running a bash script that calls mysql. The password ist not correctly transmitted, I guess I have to escape some special chars, like the hash or the dollar sign?
我正在运行一个调用 mysql 的 bash 脚本。密码未正确传输,我想我必须转义一些特殊字符,例如哈希或美元符号?
#!/bin/bash USER=myuser PASS="#mypass$" # ... call mysql
采纳答案by DarkDust
Using "..."is already the correct thing to do, but the $needs to be escaped (\$) if it isn't followed by an "invalid" character. However you also need to make sure to always have the variable in quotation marks as well, as in:
使用"..."已经是正确的做法,但是如果后面没有跟“无效”字符,则$需要对其进行转义 ( \$)。但是,您还需要确保始终将变量放在引号中,例如:
somecommand -p "$PASS"
回答by J.P
Try to use "\" before the character that you are trying to escape.
尝试在要转义的字符之前使用“\”。
#!/bin/bash
USER=myuser
PASS="#mypass$"
# ... call mysql

