Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am working on a script which dynamically executes some queries on daily basis. These queries are coming from a table in the database.

Here is the sample output of the query table:

| query_name    | query                         | userid  |
| All_User      | select * from users LIMIT 10; | jmatthe |

Now, I have to execute the query select * from users LIMIT 10; dynamically. I am reading each line of the output and storing the query from the output.

query_name=$(echo $query | cut -d\| -f1)
query_sql=$(echo $query | cut -d\| -f2)
query_user=$(echo $query | cut -d\| -f3)

Now here is where the problem arises. Because my line contains a * character in it, echo $query expands the * to replace it with the files in the current directory. So basically, my query_sql stores something like this.

select batchemail.sh query_output.txt from tbl_query

I want to preserve the * in the line so that I get the same in my query_sql variable. I want my query_sql variable to store the original data.

select * from tbl_query

Can anybody guide me on this?

share|improve this question
In zsh: noglob echo * – Emanuel Berg Apr 16 '13 at 23:10
up vote 1 down vote accepted

Use set -f to disable globbing:

$ q='select * from users LIMIT '
$ set -f
$ echo $q
select * from users LIMIT
share|improve this answer
Thanks. Too bad. I thought escaping the * would help me, but now the query doesn't work. – divinedragon Apr 16 '13 at 11:22
I have a strange feeling... How do you run the query? – TNW Apr 16 '13 at 16:02

Use double quotes around the variable:

query_name=$(echo "$query" | cut -d\| -f1)
query_sql=$(echo "$query" | cut -d\| -f2)
query_user=$(echo "$query" | cut -d\| -f3)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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