In ls -i '/home/user/Desktop/file'
, the shell removes the quotes around /home/user/Desktop/file
before passing it to ls
. ls
gets /home/user/Desktop/file
as the argument.
When you input '/home/user/Desktop/file2'
to read file
, those quotes are retained in the variable. Then, in ls "$file"
, bash expands the variable, and removes the quotes which are not a result of variable expansion (so the "..."
which were originally present in the command, but not the '...'
from the variable's contents). ls
gets '/home/user/Desktop/file2'
as the argument, and of course, there's no such file.
You don't need to additionally have quotes inside a variable. If you want to enter a filename (including spaces and backslashes, etc.) more safely using read
, use this instead:
IFS= read -r file
And as the input:
/home/user/Desktop/file2