I execute a MySQL statement with an asterisk from within a bash shell:
query=`cat <<EndOfMySQL
INSERT tmp_table
SELECT * FROM table
;
EndOfMySQL
`
echo $query
echo $query | mysql database
The problem is that the asterisk is replaced by the list of files in the current directory and the query becomes erroneous. How to avoid this behavior? It doesn't matter whether I use backticks or $()
. I'd expect some escape sequence like \*
, but at least this one doesn't work.
The * is a wildcard in Bash, it means "all files in the current directory". If you want to pass an asterisk as an argument to your program, you do it the same way you do it with every other program: you escape it with a backslash or quote it.
To use here-document in any bash script, you have to use the symbol << followed by any delimiting identifier after any bash command and close the HereDoc by using the same delimiting identifier at the end of the text.
You could prevent parameter expansion, command substitution, and arithmetic expansion in a here-document by quoting the delimiter string:
... <<\EndOfMySQL
...
EndOfMySQL
Escaping the single character will also prevent the expansions listed above.
Edit: Note that the lines of the here-doc are no subject to filename generation (globbing)!
I suppose that the problem in this case is that you didn't quote the variable passed to mysql:
echo $query | mysql database
should be:
echo "$query" | mysql database
or better yet:
printf '%s\n' "$query" | mysql database
Why don't you use:
query='INSERT into tmp_table
SELECT * FROM table;'
printf '%s\n' "$query" | mysql
or (if your shell supports here-strings, recent versions of bash support them):
mysql <<< "$query"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With