Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape strings in bash script

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
like image 727
mit Avatar asked Sep 28 '26 03:09

mit


2 Answers

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"
like image 80
DarkDust Avatar answered Sep 30 '26 15:09

DarkDust


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

#!/bin/bash

USER=myuser
PASS="#mypass\$"

# ... call mysql
like image 42
J.P Avatar answered Sep 30 '26 17:09

J.P