什么时候应该在双引号的内部/外部使用通配符? [英] When should wildcards be inside / outside double quotes?

查看:111
本文介绍了什么时候应该在双引号的内部/外部使用通配符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用通配符和变量删除许多文件的正确方法是什么?这是我的变量"$dir"

What is the correct way to use a wild card and a variable to delete many files? This is my variable "$dir"

$ echo "$dir"
/home/path/to/file

在这里,我在双引号中包含通配符,但是它不起作用:

Here I have the wild card inside the double quotes, but it does not work:

$ rm "$dir/data/ffg_per_product/ffg*"
rm: cannot remove `/home/path/to/file/data/ffg_per_product/ffg*': No such file or directory

在这里,我在双引号之外有通配符,并且可以使用:

Here I have the wildcard outside the double quotes and it works:

$ rm "$dir/data/ffg_per_product/ffg"*

在这里您可以看到文件已删除:

And here you can see the files were deleted:

$ ls -lth ffg_per_product/ffg* | wc -l
ls: cannot access ffg_per_product/ffg*: No such file or directory
0

那么我想知道的是,我是否使用引号正确地删除了通配符超过引号的文件rm "$dir/data/ffg_per_product/ffg"*?还是有另一种/更好的方式?

So what I want to know, is am I using the quotes correctly to delete the files rm "$dir/data/ffg_per_product/ffg"* with the wildcard outise the quotes? Or is there another/better way?

注意:对于某些人来说可能很明显,但只是出于参考目的,而且要明确,ls也是如此,例如ls "$dir/data/ffg_per_product/NAME"* | wc -l,也就是说,通配符是否必须在双引号之外.

NOTE: probably obvious to some but just for refererence and to be clear, the same applies for ls e.g. ls "$dir/data/ffg_per_product/NAME"* | wc -l, in that, does the wild card have to be outside the double quotes.

推荐答案

引号外的*由外壳扩展为匹配的文件名.

A * outside of quotes is expanded by the shell to matching filenames.

引号内的*不会被扩展,它实际上是一个简单的*.

A * inside quotes is not expanded, it is used literally, just a simple *.

这是正确的,并且符合您的意图:

This is correct and corresponds to your intention:

rm "$dir/data/ffg_per_product/ffg"*

使用ls命令的其他示例也是如此, 完全相同的推理.

The same goes for your other example with the ls command too, exactly the same reasoning.

这篇关于什么时候应该在双引号的内部/外部使用通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆