Insert Data to Mysql Table Using Bash Command Line

To insert data from bash command line you will need username and password to your MySQL database. Consider a following table called “fruits” with auto increment “id”:

Next, we have a file with some data:
$ cat /tmp/fruits
apple
banana
lemon

for i in $( cat /tmp/fruits ); do echo “INSERT INTO fruits (fruit) values ($i);” | mysql -u root -p password mydatabase; done

Advertisements