When I try to passing arguments as variables to any commands in bash I can see extra quotes added by bash if the variable value has spaces.
I am creating a file "some file.txt" and adding it to a variable $file. I am using $file and storing it in another variable $arg with quotes on $file. The the command I am hoping for after variable expansion by bash was
find . -name "some text.txt"
but I got error and actual file that got executed is,
find . -name '"some' 'file.txt"
Why is this happening. How bash variable expanson works in this case?
$ touch "some file.txt"
$ file="some file.txt"
$ arg=" -name \"$file\""
$ find . $arg
find: paths must precede expression: file.txt"
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
$ set -x
$ find . $arg
+ find . -name '"some' 'file.txt"'
find: paths must precede expression: file.txt"
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
Why this is happening?