-1

I was just writing a bash script to execute and I tried to cp some files from within the script; eg

cp "myfolder/*.*" "otherfolder"

and it complained that "myfolder/." could not be found. Then I tried this to be more specific

cp "myfolder/*.ini" "otherfolder"

still complaining.

then I tried

cp "myfolder/${*}.ini" "otherfolder"

still complaining; all this did was put the command line arguments in this place with .ini suffix.

cp "myfolder/{*}.ini" "otherfolder"

Still no success. So just ended putting in the full file names for each file and it worked. But is there a way to use the asterisk in a bash file to refer to all files in the directory? Or can anybody see what I'm doing wrong?

Thanks.

1 Answers1

3

Filename globbing patterns will not be expanded by the shell if you quote them (this is one of the reasons you do want to quote variable expansions in general, especially if they hold values that could be interpreted as globbing patterns).

Instead of

cp "myfolder/*.*" "otherfolder"

you should use

cp "myfolder"/*.* "otherfolder"

This would copy anything that has a name with a dot in it in myfolder to otherfolder (assuming otherfolder is the name of a directory). Note that the * pattern does not match hidden filenames by default and that most shells would leave the pattern unexpanded if no filename matched it.

Likewise, cp "myfolder/*.ini" "otherfolder" should have been written cp "myfolder"/*.ini "otherfolder".

The $* variable is quite another thing from the * globbing pattern and the two are not really related. The string "$*" will be the concatenation of the positional variables (the "command line arguments", $1, $2, $3, ...), delimited by the first character of the shell variable IFS (which by default is a space).

ilkkachu
  • 138,973
Kusalananda
  • 333,661