Asterisk conflict problem in bash quoted-strings

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

  1. macrumors newbie

    dickguertin

    Joined:
    Sep 4, 2010
    Location:
    Mountain View, CA
    #1
    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
    #!/bin/bash
    src="/Users/dickguertin/Desktop/alpha"
    dst="/Users/dickguertin/Desktop/beta"
    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. macrumors 68040

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

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

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

Share This Page