Answer the question
In order to leave comments, you need to log in
Escaping quotes. Bash to mysql query
Good afternoon, I'm just tormented, I don't know what to do.
There is a simple request (for example)
UPDATE `my_db`.`Vlan` SET `number`='11' WHERE `id`='11';
I'm trying to do it like
#!/bin/bash
query="UPDATE `my_db`.`Vlan` SET `number`='11' WHERE `id`='11';"
mysql -u root -e "$query";
/etc/script: line 2: my_db: command not found
/etc/tdma.sh: line 2: Vlan: command not found
/etc/tdma.sh: line 2: number: command not found
Answer the question
In order to leave comments, you need to log in
Backticks (`) in bash execute the command enclosed in them. Try without those quotes.
It is not always possible to simply remove the quotes (for example, if '-' is used in the names of the database, tables, fields). Here it is better to replace double quotes " with single quotes '. Within them, ` is not treated as exec.
Bash has an absolutely insane quote escaping system, but maybe this will help you: wiki.bash-hackers.org/syntax/quoting#ansi_c_like_strings
In general, I would put queries in separate files so as not to suffer.
Faced a similar problem and solved this issue with normal escaping:
query="CREATE DATABASE \`${site_name}\`;"
mysql -u ${mysql_user} -p${mysql_password} -e "${query}"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question