Why is "find: paths must precede expression" error triggered when multiple results are returned from "find"

Why is the error: paths must precede: trigger.txt when multiple results are returned from "find" in a subprocess, but not when a single result is returned?

The directory contains three files.

ls
input2.txt  input.txt  input.log 

      

There is only one file matching the search query and the result can be assigned to $ foo

$ foo=$(find . -name *.log )
echo $foo
./plot.log

      

When > 1

results are returned, look for the throw error.

$ foo=$(find . -name *.txt )
find: paths must precede expression: input.txt

      

I don't understand why this is happening.

+3


source to share


1 answer


You need quote special characters as globs are expanded before running the command:

find . -name '*.txt'

      



To see how globbing works, try for example echo *.txt

- it will actually print *.txt

if there are no files in the current directory ending with .txt

.

+13


source







All Articles