开发者

How to escape strings in bash script

开发者 https://www.devze.com 2023-02-15 02:25 出处:网络
I am running a bash script that calls mysql. The pa开发者_运维知识库ssword ist not correctly transmitted, I guess I have to escape some special chars, like the hash or the dollar sign?

I am running a bash script that calls mysql. The pa开发者_运维知识库ssword 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


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"


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

#!/bin/bash

USER=myuser
PASS="#mypass\$"

# ... call mysql
0

精彩评论

暂无评论...
验证码 换一张
取 消