我正在阅读下面的教程
http://linuxcommand.org/lc3_lts0080.php
关于双引号:
Double Quotes
The first type of quoting we will look at is double quotes. If you place text inside double quotes, all the special characters used by the shell lose their special meaning and are treated as ordinary characters. The exceptions are “$”, “\” (backslash), and “`” (back- quote). This means that word-splitting, pathname expansion, tilde expansion, and brace expansion are suppressed, but parameter expansion, arithmetic expansion, and command substitution are still carried out.
关于单引号:
Single Quotes
If you need to suppress all expansions, you use single quotes.
它确实用 ls
解释了为什么只有 *.txt
不工作 "*.txt"
或 '*。 txt'
$ ls -l *.txt
-rw-r--r-- 1 Gaurav None 8893 Apr 17 06:25 BigText.txt
-rwxr-xr-x 1 Gaurav None 54376 Apr 18 12:32 File.txt
-rw-r--r-- 1 Gaurav None 371 Apr 23 21:04 Filelist.txt
-rwxr-xr-x 1 Gaurav None 54386 Apr 18 12:28 FileOld.txt
-rw-r--r-- 1 Gaurav None 1163 Apr 17 06:19 Read.txt
-rw-r--r-- 1 Gaurav None 651 Apr 24 06:28 temp.txt
$ ls -l "*.txt"
ls: cannot access *.txt: No such file or directory
$ ls -l '*.txt'
ls: cannot access *.txt: No such file or directory
我的问题是,使用 find 或 grep
为什么 "*.txt", '*.txt'
都提供扩展:(事实上 * .txt
没有为名称提供任何扩展,与 ls
)
例如
$ find . -name *.txt
find: paths must precede expression: File.txt
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
$ find . -name "*.txt"
./BigText.txt
./File.txt
./Filelist.txt
./FileOld.txt
./Read.txt
./sample/BigText.txt
./sample/File.txt
./sample/FileOld.txt
./sample/Read.txt
./sample/temp.txt
./temp.txt
$ find . -name '*.txt'
./BigText.txt
./File.txt
./Filelist.txt
./FileOld.txt
./Read.txt
./sample/BigText.txt
./sample/File.txt
./sample/FileOld.txt
./sample/Read.txt
./sample/temp.txt
./temp.txt
最佳答案
find
自己扩展*
– 它不需要 shell 来完成。
您需要了解在 shell 中键入命令(或 shell 从脚本执行命令时)会发生什么:
首先,所有的扩展都是由shell完成的,比如变量扩展(把$foo
变成foo
的值),波浪符扩展(把~
变成主目录)路径)和通配符(将 *
转换为所有匹配的文件)。
然后,在完成所有扩展后,扩展后的值作为命令执行。
当您调用 find
时,您实际上不希望 shell 扩展 *
你传递给find
作为模式的一部分,因为 find
命令解释 *
本身并将其用作搜索模式(如 find <dir> -name '*.foo'
)。
关于linux - 在 Linux 中扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23354104/