Super User is a question and answer site for computer enthusiasts and power users. 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 have a script which takes a filename as a parameter, for example "mydb.sql". My script imports this DB and "cleans" it (removes user identifiable data, etc), exports it and then creates a "lite" version (strips all non-essential data out, leaving only main admin user, removing all votes, etc). These get generated into mydb.clean.sql and mydb.lite.sql. This script gets run on several folders.

I want to create a wildcard pattern to pass into find. Originally I just hard coded '*.sql' which returned the original, clean and lite (these were then passed into du to give me a file size summary).

Now I have altered my script to take a parameter; the filename. This allows me to run it on a single file.

The Question

So how do I get the pattern? I have this:

FNAME=$(basename "$1");
echo $FNAME

This converts FNAME from /home/user/backups/sitea/mydb.sql to mydb. I then want FNAME to be the string 'mydb*.sql', however bash is expanding that * out so I end up with FNAME as 'mydb.sql mydb.clean.sql mydb.lite.sql' (assuming it found them in the CWD).

I have tries backslashing the *, however I then end up with 'mydb\*.sql' which doesn't work. I tried using single quotes, but then bash doesn't expand the ${FNAME%.*} part.

Any thoughts?


This better illustrates the issue...

[user@server temp]$ T="${FNAME}\*.sql"; echo $T

[user@server temp]$ T="${FNAME}*.sql"; echo $T
mydb.clean.sql mydb.lite.sql mydb.sql

[user@server temp]$ T='${FNAME}*.sql'; echo $T
share|improve this question
up vote 3 down vote accepted

It's being expanded at the last step.

echo "$FNAME"
share|improve this answer
Looks like you're right... T="${FNAME}*.sql"; echo "$T outputs: mydb*.sql – Nick Jul 16 '12 at 13:27
Of course - it makes sense now. Unquoted variables get expanded. I simply had to add double quotes to the echo and the find calls and it worked perfectly. Thanks @Ignacio – Nick Jul 16 '12 at 14:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .