I would like to call a .sql file from a .sh file in linux. The .sql file will contain queries to MySQL database.

I am able to call the .sql file by running .sh file in linux, but the sql query is not working. Below is my syntax. The select statement is throwing below error.

./sample.sql: line 4: syntax error near unexpected token `from'

sample.sh file:

echo "this is shellscript calling sql"

sample.sql file:

set pages 999
set lines 999
set colsep |
select * from sampletable;

expected result should be similar to a normal select * from xxx statement


Instead of sourcing the sql-file, you need to pass it to mysql:

mysql -u user -psecret < /full/path/to/sample.sql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.