How to escape strings in bash script

99封情书 提交于 2019-12-10 22:53:44

问题


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?

#!/bin/bash

USER=myuser
PASS="#mypass$"

# ... call mysql

回答1:


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"



回答2:


Try to use "\" before the character that you are trying to escape.

#!/bin/bash

USER=myuser
PASS="#mypass\$"

# ... call mysql


来源:https://stackoverflow.com/questions/5220325/how-to-escape-strings-in-bash-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!