You have to quote your variable:
Otherwise the shell expands the
* to the files in the current directory.
echo "*" and
echo * in your shell.
* is part of a feature of your shell (probably Bash) called Pathname Expansion.
From the bash man page:
The special pattern characters have the following meanings:
* Matches any string, including the null string. When the globstar shell option
is enabled, and * is used in a pathname expansion context, two adjacent *s used
as a single pattern will match all files and zero or more directories and subdi-
rectories. If followed by a /, two adjacent *s will match only directories and
To prevent this you can escape the
* with a backslash
\, or quote it with double quotes
" or single quotes
# no pathname expansion
$ echo \*
$ echo "*"
$ echo '*'
# pathname expansion
$ echo *
file_x file_y file_z_in_this_directory
The problem with single quotes is, that it would prevent the expansion of your variables (also called parameter expansion). All characters are interpreted literally.
# no variable expansion
$ echo '$QUERY'
# variable expansion
$ echo "$QUERY"
select * from foo
# variable expansion and pathname expansion
$ echo $QUERY
select file_x file_y file_z_in_this_directory from foo