views:

439

answers:

4

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.

+4  A: 

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.

gregseth
So if I want to do it I should do FILES=".*bash*"? But what if I want hidden AND not hidden files?
sixtyfootersdude
+4  A: 
alanc
+4  A: 

The default globbing in bash does not include filenames starting with a . (aka hidden files).

You can change that with

shopt -s dotglob

[22:38:07 0 ~/foo] $ ls -a
.  ..  .a  .b  .c  d  e  f
[22:38:11 0 ~/foo] $ ls *
d  e  f
[22:38:12 0 ~/foo] $ shopt -s dotglob
[22:38:18 0 ~/foo] $ ls *
.a  .b  .c  d  e  f
[22:38:20 0 ~/foo] $ 
nos
+1  A: 

@OP if you want hidden and non hidden, set dotglob (bash)

#!/bin/bash
shopt -s dotglob
for file in *
do
 echo "$file"
done
ghostdog74