# variable in grep regex



## Just_Johnny (Nov 1, 2009)

I'm trying to write a script that counts number of failed logins each day.  


```
#get date in format "Month Day"
DATE=`date +%b" "%d`

#remove whitespace from date
DATE=`expr "$DATE" : '[[:space:]]*\(.*\)[[:space:]]*\(.*\)[[:space:]]*$'`

#count failed logins for today
WC=$(grep -e '^.*$DATE.*FAIL LOGIN.*$' /var/log/file.log | wc -l)
```

my probem is 

WC=$(grep -e '^.*$DATE.*FAIL LOGIN.*$' /var/log/file.log | wc -l)

is not the same as

WC=$(grep -e '^.*Oct 31.*FAIL LOGIN.*$' /var/log/file.log | wc -l)

$DATE outputs Oct 31 but I cannot get this to work when I use a variable

Everything works great if I just statically input the date string

Could somebody PLEASE help me?


----------



## crsd (Nov 1, 2009)

```
WC=$(grep -e "^.*$DATE.*FAIL LOGIN.*$" /var/log/file.log | wc -l)
```

Variable substitution doesn't work inside '' (single quotes), AFAIK.


----------

