这个 bash 脚本如何知道我当前目录中的文件?

How does this bash script know the files in my current directory?

在阅读 Bash Reference Manual 时,我遇到了以下示例:

select fname in *;
do
    echo you picked $fname \($REPLY\)
    break;
done

脚本的输出如下:

1) case1.sh
2) loop1.sh
3) loop2.sh
4) select1.sh
#? 3
you picked loop2.sh (3)

给出的所有选项都是我运行脚本来自(select1.sh)的目录的文件。 我的问题是脚本如何知道我当前工作目录的内容?

我的猜测是 * 令牌对此负责。

* 是一个 glob (wildcard pattern) that expands to the names of all the files in the current working directory (excluding hidden files by default)。您可以通过 运行 echo * 轻松演示这一点,它应该会产生以下输出:

case1.sh loop1.sh loop2.sh select1.sh

这在 Filename Expansion 下的 Bash 手册中有记载。