1

I know that to deal with spaces in file names you should wrap the entire file name between quotes but this is not working for this script, indeed what is happening is the exact opposite, it only works without using quotes and I don't understand why. Tested with gnome-terminal.

echo "Drag and drop some file"
read FILE
NAME=$(basename "$FILE")
cp "$FILE" ~/foo/bar/"$NAME" && echo "Success!"
12
  • What, exactly, do you enter as $FILE? The only suggestion I can make to this code is to read -r FILE Sep 13, 2022 at 1:14
  • You say "it only works without using quotes", but you have quotes in all the right places (where you're trying to get the value inside the FILE variable). Were you trying to put quotes around the FILE variable name in the read FILE line or the NAME variable name in NAME=$(basename ...)?
    – Sotto Voce
    Sep 13, 2022 at 1:33
  • 1
    "What, exactly, do you enter as $FILE?": if I type my file.txt it works but "my file.txt" or 'my file.txt' doesn't
    – user541146
    Sep 13, 2022 at 1:45
  • What's your actual question? Your script works in GNU bash, version 5.1.16, if you just remove the extraneous backticks.
    – appas
    Sep 13, 2022 at 1:50
  • "but you have quotes in all the right places": The drag and drop function adds single quotes, it's about these quotes that I'm referring, as explained in this answer: stackoverflow.com/a/32060406
    – user541146
    Sep 13, 2022 at 1:51

1 Answer 1

1

Hopefully an example will illustrate the difference: the stuff between the single quotes is like what you enter at the read command

Entering a name without quotes:

f='file 1'
touch "$f"
ls --literal -l "$f"
-rw-r--r-- 1 glennj glennj 0 Sep 13 01:57 file 1

Entering a name with quotes:

f='"file 2"'
touch "$f"
ls --literal -l "$f"
-rw-r--r-- 1 glennj glennj 0 Sep 13 01:57 "file 2"

The double quotes are literal characters in this case: they are actually part of the filename.

When you use quotes in what you enter at read, the quotes are literally part of the text.

You must log in to answer this question.