为什么执行''GLOBIGNORE=keepfile rm *''命令会删除keepfile文件?

3

有件事我还不是很明白:

touch a b c ; mkdir -p git ; mkdir -p iii/ooo/ppp ; touch git/rtdsfgsdg  ; touch .sdfsadf

那么,这是否产生了我想要的结果?

» tree -a
.
├── a
├── b
├── c
├── git
│   └── rtdsfgsdg
├── iii
│   └── ooo
│       └── ppp
└── .sdfsadf

4 directories, 5 files

是的。一些文件,一个目录,以及一个隐藏文件。都很好。

现在我想要删除所有东西,但是我想要保留隐藏文件和git目录。 GLOBIGNORE 来拯救!

GLOBIGNORE=git rm -rf *

它进行得如何?

» tree -a
.
└── .sdfsadf

0 directories, 1 file

太糟糕了。我的git文件夹在哪里?为什么GLOBIGNORE不能按照广告所说的工作?

   GLOBIGNORE
          A  colon-separated  list  of  patterns  defining the set of file
          names to be ignored by  pathname  expansion.   If  a  file  name
          matched  by a pathname expansion pattern also matches one of the
          patterns in GLOBIGNORE, it is removed from the list of matches.

我正在使用:
» bash --version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

我们已经有现有的问答条目,提供了与紧密相关的问题“为什么var = old; var = new echo"$ var"打印old而不是new?”本质上相同的答案;但是它们很难通过搜索关键字找到。 - Charles Duffy
1个回答

6

你使用的语法只在执行时临时应用变量,而不是解析时; 因此,它不能改变全局通配符展开的方式,因为将 * 替换为文件列表是在调用 rm 之前发生的。

要修改解析时行为,请拆分成两个单独的命令:

GLOBIGNORE=git
rm -rf *

请在 https://replit.com/@CharlesDuffy2/LovablePoliticalPrograms 查看成功运行的示例。


既然你提到了,这很明显。谢谢! - DingDong

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接