Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an asterisk into bash heredoc?

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.

like image 442
Kalmar Avatar asked Nov 07 '12 10:11

Kalmar


People also ask

How do you use asterisk in bash?

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.

How do you end a HereDoc?

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.


1 Answers

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"
like image 95
Dimitre Radoulov Avatar answered Oct 07 '22 14:10

Dimitre Radoulov