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

Viewed 68

While reading the Bash Reference Manual I came across the following example:

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

The output of the script is the following:

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

All the options given are the files of the directory I'm running the script from (select1.sh). My question is how does the script know the contents of my current working directory?

My guess would be that the * token is responsible for it.

1 Answers

The * is a glob (wildcard pattern) that expands to the names of all the files in the current working directory (excluding hidden files by default). You can demonstrate this easily by running echo *, which should produce this output:

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

This is documented in the Bash manual under Filename Expansion.

Related