gpt4 book ai didi

带有通配符和隐藏文件的 Bash for 循环

转载 作者:行者123 更新时间:2023-11-29 08:44:07 25 4
gpt4 key购买 nike

只知道一个简单的 shell 脚本,有点困惑:

这是我的脚本:

% for f in $FILES; do echo "Processing $f file.."; done

命令:

ls -la | grep bash 

产生:

% ls -a | grep bash
.bash_from_cshrc
.bash_history
.bash_profile
.bashrc

什么时候

FILES=".bash*"

我得到与 ls -a 相同的结果(不同的格式)。然而当

FILES="*bash*"

我得到这个输出:

Processing *bash* file..

这不是预期的输出,也不是我所期望的。我不允许在文件名的开头使用通配符吗?是个 。在文件名“特殊”的开头不知何故?

设置

FILES="bash*"

也不行。

最佳答案

bash 中的默认 globbing 不包括以 . (又名隐藏文件)。

你可以用

改变它

shopt -s dotglob

$ ls -a
. .. .a .b .c d e f
$ ls *
d e f
$ shopt -s dotglob
$ ls *
.a .b .c d e f
$

要再次禁用它,请运行 shopt -u dotglob

关于带有通配符和隐藏文件的 Bash for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2135770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com