Home > front end >  find command in bash vs find command in cli
find command in bash vs find command in cli

Time:02-10

I need to accommodate spaces in filepaths. Why does "find" not work from the script, but works from the cli?

MyLaptop$ ./my-bash-script.sh

find: '/Sandbox/test folder/testfiles-good/ResidentFile_1.pcapng': No such file or directory

MyLaptop$ find '/Sandbox/test folder/testfiles-good/ResidentFile_1.pcapng'

/Sandbox/test folder/testfiles-good/ResidentFile_1.pcapng

Using echo find -f "'$line'"

output: find -f '/Sandbox/test folder/testfiles-good/ResidentFile_1.pcapng'

But in this case: FOUND="$(find -f "'$line'")" it does not

CodePudding user response:

In this case ...

FOUND="$(find -f "'$line'")"

... you are asking find for a file whose name contains leading and trailing ' characters. That is unlikely to be what you intended. Though it may look strange, this is probably what you meant:

FOUND="$(find -f "$line")"

The " characters inside the command substitution do not pair with those outside (and if they did then the original command substitution would be wrong a different way). On the other hand, word splitting is not performed on the result of the command substitution when it is used on the right-hand side of a variable assignment, so you could also just use

FOUND=$(find -f "$line")
  • Related