oscarmlage oscarmlage

Call date inside an alias

Written by oscarmlage on

Stupidity of the day: calculate the date in an double quoted bash alias. It seems that, if you don't escape the date call, it's called at the time of the alias definition, so it's not the real behaviour I was looking for:

$ date +%Y%m_%H%M
201507_2252
$ alias foo="echo `date +%Y%m_%H%M`"
$ foo
201507_2252

Wait a minute and...

$ date +%Y%m_%H%M
201507_2253
$ foo
201507_2252

Not really usable when you want the date at the time you run the alias, not when it's defined, so you have to escape the date call this way:

$ date +%Y%m_%H%M
201507_2253
$ alias foo="echo `date +%Y%m_%H%M`"
$ foo
201507_2253

Wait a minute, or a couple of minutes and...

$ foo
201507_2255

That's all, stupid but so useful trick when you want to define something like:

alias test_cov="py.test -s --tb=native --cov proj --cov-report term-missing | tee logs/proj-`date +%Y%m_%H%M`.log"

Enjoy!