0

consider the following terminal output (inside a .sh file)

kagura@mylaptop1 : ~
=> sudo cp -a "$_source/*.txt" $_destination/Other/
Password:
cp: /home/myuser/*.txt: No such file or directory
08:06:13 Thu Jun 14
kagura@mylaptop1 : ~
=> sudo cp -a $_source/*.txt $_destination/Other/
08:06:33 Thu Jun 14
kagura@mylaptop1 : ~

The one with double quotes fails while the one without successfully runs.

I had the impression that double quoting when it comes to path/files is safe in the possibility that the path or file has a space.

Thanks for the help

7

* expands to the filenames in the folder, while "*" does not expand.

But your "impression" is right, you should quote all parts from which you are not 100% sure that there is no space in it.

You can mix quoted and non-quoted parts:

sudo cp -a "$_source"/*.txt "$_destination"/Other/
| improve this answer | |
  • 3
    Also note, if *.txt expands to include files with spaces, this will not cause any problems for cp: the shell has performed word splitting before filename expansion, and it will not do a 2nd round of word splitting after the pattern is expanded. See gnu.org/software/bash/manual/bash.html#Shell-Expansions – glenn jackman Jun 13 '18 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.