Asterisk conflict problem in bash quoted-strings

Discussion in 'Mac Programming' started by dickguertin, Dec 9, 2011.

  1. dickguertin macrumors newbie


    Sep 4, 2010
    Mountain View, CA
    I'm trying to do a: cp "$src/*.tgz" "$dst/." command in a bash script, where $src and $dst are bash variables containing the path names, which may include blanks in their names. The sources to be moved are all *.tgz files in $src, but the shell script tells me there are none. I believe "parameter expansion" is trumping "pathname expansion" and leaving the asterisk (*) such that "cp" can't interpret it. The same cp-command without quotes works, but only if both $src and $dst are non-blank strings.

    How can I accomplish this "cp"?

    pbdsl1:~ dickguertin$ cat ab
    rm -f $dst/*.tgz
    cp "$src/*.tgz" "$dst/."
    # cp $src/*.tgz $dst/.
    exit 0
    pbdsl1:~ dickguertin$ ./ab
    cp: /Users/dickguertin/Desktop/alpha/*.tgz: No such file or directory

    The non-quoted (commented) command works, but only because there are no blanks in the path names. There are alpha and beta directories on the Desktop, and alpha holds several tgz-files.
  2. subsonix macrumors 68040

    Feb 2, 2008
    Are you sure of this? This works for me.

    cp $sourcedir/*.tgz $destdir/
    If you have spaces in the path names you might have to quote the variables.

    cp "$sourcedir"/*.tgz "$destdir"/

Share This Page