Eicos
Eicos

Reputation: 35

Bash converting double quotes to single quotes

I'm having a problem with my bash script that is driving me up the wall. I've got a piece of code that's supposed to bring in some files:

if [[ $FILENODE == null ]]
then
    cp "/home/dash/reference/hg18_bowtie_build/hg18.fa*" "$JOBDIR"
    cp "$IN" "$JOBDIR"
else
    echo "Copying files from node$FILENODE"
    rcp "node$FILENODE-ib:$JOBDIR/hg18.fa*" "node$FILENODE-ib:$JOBDIR/$IN" "$JOBDIR"
fi

Usually we will be falling into the first part of the conditional, and so the cp command is supposed to expand the wildcard in the first argument. I understand I need to double quote my arguments to ensure they are expanded. However, when I execute this script with bash -x, this is what I see:

+ cp '/home/dash/reference/hg18_bowtie_build/hg18.fa*' /usr/tmp/707361.master.cl.osumc.edu
cp: cannot stat `/home/dash/reference/hg18_bowtie_build/hg18.fa*': No such file or directory
+ cp '*.sort.bam' /usr/tmp/707361.master.cl.osumc.edu
cp: cannot stat `*.sort.bam': No such file or directory

For some reason, bash seems to be not only ignoring my double quotes, but turning them into single quotes. The variables are getting expanded, but not the wildcards. If anyone can explain what is going on here, I would really appreciate it.

Upvotes: 2

Views: 1282

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

Double quotes permit parameter expansion, but they still inhibit glob expansion. Move the glob wildcards outside the quotes.

cp "/home/dash/reference/hg18_bowtie_build/hg18.fa"* "$JOBDIR"

As for the second, create an array.

files=(foo*)
cp "${files[@]}" "$bar"

Upvotes: 3

perreal
perreal

Reputation: 97948

You should not quote the file path if you want globbing:

cp /home/dash/reference/hg18_bowtie_build/hg18.fa* "$JOBDIR"

Upvotes: 4

Related Questions