I need to use the result of a formatted date command (date +"%m/%d") as the grep filter to filter the results of another command that will display alerts on a system so that I only see alerts from the given day.

Lets say alertcommand gives the alert readout in the console. I know I can do alertcommand | grep 8/10 to get the logs from today, but I want to be able to feed date +"%m/%d"into that so that I don't have to specify the date each time I run it, but I can't figure out how to link it into one single command (preferably without having to create temp files or anything as this will be run on customer hardware).

I looked at this question that was similar, but couldn't figure out how to make it work the same way.

3 Answers 3


In bash, just use something like alertcommand | grep $(date +"%m/%d")

$() executes a command in a subshell and returns the output of the command as string. Alternatively you can enclose the command with backticks to the same effect.

  • This worked, seems I was just missing the format of how to write it out from the other question I noted. I prefer to use $() as it is the more modern implementation.
    – MaQleod
    Commented Aug 10, 2011 at 21:16

You could simply assign the output of your date command to another variable, and use that as an argument:

myDateVariable=`date +"%m/%d"`
alertcommand | grep $myDateVariable

This way, you could re-use the date value in the future and also debug any intermediate steps

  • Not as useful to me as a single line command, but still a valid method.
    – MaQleod
    Commented Aug 10, 2011 at 21:17
  • You can always separate them via a semicolon. For example, you could say myDateVariable=`date +"%m/%d"`; alertcommand | grep $myDateVariable Commented Aug 11, 2011 at 8:19

In FreeBSD's shell csh I can't use both of yours methods.


cat /var/log/exim/mainlog | grep $(date +%Y-%m-%d)
Illegal variable name.


d=`date +%Y-%m-%d`;cat /var/log/exim/mainlog|grep $d
d=2011-11-13: Command not found.

But I can use directly backticked code like this:

cat /var/log/exim/mainlog | grep `date +%Y-%m-%d`

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .