Search code examples
bashloopsscriptingwildcard

Bash for loop with wildcards and hidden files


Just witting a simple shell script and little confused:

Here is my script:

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

The Command:

ls -la | grep bash 

produces:

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

When

FILES=".bash*"

I get the same results (different formatting) as ls -a. However when

FILES="*bash*"

I get this output:

Processing *bash* file..

This is not the expected output and not what I expect. Am I not allowed to have a wild card at the beginning of the file name? Is the . at the beginning of the file name "special" somehow?

Setting

FILES="bash*"

Also does not work.


Solution

  • FILES=".bash*" works because the hidden files name begin with a .

    FILES="bash*" doesn't work because the hidden files name begin with a . not a b

    FILES="*bash*" doesn't work because the * wildcard at the beginning of a string omits hidden files.