linux - 在 Linux 中扩展

标签 linux shell

我正在阅读下面的教程

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/

相关文章:

linux - 如何从文件中添加对话框菜单项?

linux - 启动 xampp 时出现错误,错误缺少作业名称

json - 如何合并 bash shell 中唯一的对象 TargetGroupARNs 和 TargetGroupArn?

shell - tcsh:foreach 命令在终端中运行良好,但在从 tcsh 脚本文件调用时不起作用

mysql - mysql 批处理文件中的 Tee 命令不起作用

linux - 如何在 64 位 Linux 上从 32 位 Wine 执行 shell 脚本?

ios - 在 iOS 上使用蓝牙

linux - 'git clone' 遵循 umask,顶级项目目录除外

mysql - 如何在 bash 脚本中授予 MySQL 权限?

python - python 中的空格字符不会被 bash 解释