How to execute SQL command with parameters in bash script

I would like to make a bash script that will add a user to my user table in a MySQL database. I have a username as a script argument stored in the $ USERNAME variable, and I need to execute this query on a MySQL server:

INSERT INTO `user` VALUES ('$USERNAME', password) 

I know how to execute sql file, but it will not help me when I have username in a variable. The only way I can think of is to execute a php file with GET values ​​and execute the sql command in php. And I'm not sure what will work with php, as I think.

Is there a better way?

thanks

EDIT: After your helpful answers, I went with this command, which works:

 mysql -ppassword --default-character-set=utf8 -e "INSERT INTO table VALUES (\" $USERNAME@ \",\"password\")" database 
+1
source share
2 answers

Command:

 mysql -pyourpasswordwithoutspaces -e "Your insert query goes here with semicolon;" 

I'm not sure if semicolons are needed or not, but it's always helpful to provide them.

0
source

You can try using heredoc:

 #!/bin/bash USERNAME="example" mysql <<MYSQL INSERT INTO user VALUES ('$USERNAME', password); MYSQL 
+1
source

Source: https://habr.com/ru/post/976776/


All Articles