When you drag and drop a fille in your graphical environment, the shell probably includes additional characters (such as quotes) in the string it provides. Your script tries to find a file that is exactly what the shell provides (i.e. quotes included) and does not find one.
Initial solution - Not recommended, but left here because it worked for the original poster
You could try this :
read input
printf -v input "$(eval echo "$input")"
That printf
statement does two things : it uses the -v input
to assign a new value to the input
variable. The value it assigns is the output of eval echo "$input"
, captured with a command substitution $()
. eval
forces expansion to be performed on the arguments it is passed before executing them as a command. The command ends up being echo
with its value being the (expanded) initial value of variable input
.
Remember : read
does not perform expansion or word splitting, if just takes what you type and puts in in a variable, as is.
Please note that use of eval
opens up code injection risks. A specially crafted value could cause arbitrary code execution by eval
, and the code I am proposing does not protect from that. There are safer, better ways to do that if the exact format of the string sent to the terminal window is known.
Update - Recommendation based on additional details from original poster
For instance, if you know the file name is inside single quotes, you could do this (which will also ignore leading and trailing whitespace) :
read input
if
[[ $input =~ ^[[:space:]]*\'(.*)\'[[:space:]]*$ ]]
then
input="${BASH_REMATCH[1]}"
fi
This code snippet will extract the file name from inside the single quotes if they are there, or will leave the input as is otherwise, and is safer from a code injection standpoint.