<p>I know that with mysql you can write SQL statements into a .sql file and run the file from the mysql command line like this:</p>

<pre><code>mysql&gt; source script.sql
</code></pre>

<p>How do I pass a variable to the script? For example, if I want to run a script that retrieves all the employees in a department, I want to be able to pass in the number of the department as a variable.</p>

<p>I am not trying to run queries through a shell script. There are simple queries I run from the mysql command line. I'm tired of retyping them all the time, and writing a shell script for them would be overkill.</p>

[{'answer_id': 76124, 'author': 'Brad Choate', 'author_id': 553, 'author_profile': 'https://Stackoverflow.com/users/553', 'pm_score': 6, 'selected': True, 'text': "<p>Like this:</p>\n\n<pre><code>set @department := 'Engineering';\n</code></pre>\n\n<p>Then, reference <code>@department</code> wherever you need to in script.sql:</p>\n\n<pre><code>update employee set salary = salary + 10000 where department = @department;\n</code></pre>\n"}, {'answer_id': 76287, 'author': 'longneck', 'author_id': 8250, 'author_profile': 'https://Stackoverflow.com/users/8250', 'pm_score': -1, 'selected': False, 'text': "<p>you really should be looking at a more appropriate way of doing this. i'm going to guess that you're trying to run mysql queries via a shell script. you should instead be using something like PERL or PHP.</p>\n"}, {'answer_id': 25109187, 'author': 'Yordan Georgiev', 'author_id': 65706, 'author_profile': 'https://Stackoverflow.com/users/65706', 'pm_score': 5, 'selected': False, 'text': '<pre><code>    #!/bin/bash\n\n    #verify the passed params\n    echo 1 cmd arg : $1\n    echo 2 cmd arg : $2\n\n    export db=$1\n    export tbl=$2\n\n    #set the params ... Note the quotes ( needed for non-numeric values )\n    mysql -uroot -pMySecretPaassword \\\n    -e "set @db=\'${db}\';set @tbl=\'${tbl}\';source run.sql ;" ;\n\n    #usage: bash run.sh my_db my_table\n    #\n    #eof file: run.sh\n\n    --file:run.sql\n\n    SET @query = CONCAT(\'Select * FROM \', @db , \'.\' , @tbl ) ;\n    SELECT \'RUNNING THE FOLLOWING query : \' , @query ;\n    PREPARE stmt FROM @query;\n    EXECUTE stmt;\n    DEALLOCATE PREPARE stmt;\n\n    --eof file: run.sql\n</code></pre>\n\n<p>you can re-use the whole concept from <a href="https://github.com/YordanGeorgiev/mysql-starter" rel="noreferrer">from the following project</a></p>\n'}]
['https://Stackoverflow.com/questions/76065', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13519/']