简体   繁体   中英

what is the difference between something and `something` in the linux shell?

I want to find all the .pdf files recursively by using find

So I typed in find . -name *.pdf find . -name *.pdf

And the output was weird ,it only contains all the pdf files in the current directory , other pdf fils in the sub directory is omitted

Then I tried this find . -name '*.pdf' find . -name '*.pdf'

This time ,every thing is fine .And I got what I want, I mean all the pdf files including those located in the sub directory.

So here comes the deal: what is the difference between find . -name *.pdf find . -name *.pdf and find . -name '*.pdf' find . -name '*.pdf'

Yes as you've found that quoting makes all the difference there.

Without quoting *.pdf gets expanded by shell glob expansion rule even before find runs and thus find command shows all the pdf files from that list only.

In other words this find command:

find . -name *.pdf

is same as:

printf "%s\n" *.pdf

So right way to use find is:

find . -name '*.pdf'

In the Linux shell (bash, csh, sh, and probably many others I'm not as familiar with), different quotes mean different things.

Fundamentally, quotes do two things for you:

  1. Limiting text substitution : You're experiencing the shell substituting references to all PDF files in your current directory for *.pdf . That's an example of text substitution. Text substitution can also occur with variable names--for example:

     bash$ MYVAR=test bash$ echo $MYVAR test bash$ echo '$MYVAR' $MYVAR 

    will give you the output test , because $MYVAR is substituted with the value the variable is set to.

  2. Overriding space as an argument separator : Let's pretend you have a directory with these files

     bash$ ls -1 file1 file1 file2 file2 

    If you type ls file1 , you'd get file1 as you expect. Similarly, ls file2 gives you file2 . The following commands show the significance of quotes overriding space as an argument separator:

     bash$ ls -1 file1 file2 file1 file2 bash$ ls -1 "file1 file2" file1 file2 

    Notice how the first example displays two files file1 and file2 , while the second example displays one file file1 file2 . That's because the quotes stop " " (a single space) from being used as an argument separator.

One final note: your original question asks about the difference between something and 'something' . It's worth nothing that there is actually a difference between something , 'something' , and "something" . Consider this:

bash$ MYVAR=test
bash$ echo $MYVAR
test
bash$ echo '$MYVAR'
$MYVAR
bash$ echo "$MYVAR"
test

Note the difference between '$MYVAR' and "$MYVAR" . The ' (single quote) is considered a "strong quote," meaning everything contained inside it is explicit. The " (double quote) is a "weak quote," which does not expand * or ? , but does expand variable names and command substitutions.

The Grymorie provides a tremendous amount of information about quotes. Have fun learning!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM