1

First off, I have read the post here: Bash alias create file with current timestamp in filename

However, when I run it, I keep getting the same timestamp. This makes no sense to me.

I am running Git bash (From git 2.16.2) on Windows 10. I have added the following line into my ~/.bash_aliases: alias logfile="adb logcat -v threadtime *:V | tee /c/Users/username/Desktop/adb-logs/'$(date +%Y-%m-%d@%Hh%Mm%Ss)'-FullLogCat.log"

This seems to create a log file and display the ADB logs on screen, as I want. However, the actual date seems to be "stuck" at the same time as the terminal is loaded. For example, I load the terminal at 2:29:09pm then wait until 2:30:00pm to call the alias, the file gets stamped as "2018-04-24@14h29m09s-FullLogCat.log". It seems like as the profile loads the aliases into memory, it executes the date function.

Is there a way to prevent this? Is this just a weird git bash thing?

Update: Solution

alias logfile='adb logcat -v threadtime *:V | tee /c/Users/ghannan/Desktop/adb-logs/''$(date +%Y-%m-%d@%Hh%Mm%Ss)''-FullLogCat.log
Gordon H
  • 11
  • 3

3 Answers3

3

This should really be a function (as the question you should ask yourself is "Is there any reason this can't be a function?"):

logfile () {
    dt=$(date +%Y-%m-%d@%Hh%Mm%Ss)
    output="/c/Users/username/Desktop/adb-logs/$dt-FullLogCat.log"
    adb logcat -v threadTime *:V | tee "$output"
}

From the bash man page:

For almost every purpose, aliases are superseded by shell functions.

chepner
  • 497,756
  • 71
  • 530
  • 681
1

You need to escape the $ in the alias so that the date command runs each time you invoke the alias. Currently it is only being run when you are creating the alias.

EDIT: You also need to remove the single quotes around the date expansion as others in this thread have pointed out.

$ alias logfile="adb logcat -v threadtime *:V | tee /c/Users/username/Desktop/adb-logs/'$(date +%Y-%m-%d@%Hh%Mm%Ss)'-FullLogCat.log"
$ alias | grep adb
alias logfile='adb logcat -v threadtime *:V | tee /c/Users/username/Desktop/adb-logs/'\''2018-04-24@13h39m08s'\''-FullLogCat.log'

You can see above, without escaping $ the date is hardcoded into the alias.

$ alias logfile="adb logcat -v threadtime *:V | tee /c/Users/username/Desktop/adb-logs/\$(date +%Y-%m-%d@%Hh%Mm%Ss)-FullLogCat.log"
$ alias | grep adb
alias logfile='adb logcat -v threadtime *:V | tee /c/Users/username/Desktop/adb-logs/'\''$(date +%Y-%m-%d@%Hh%Mm%Ss)'\''-FullLogCat.log'

In this example, we have escaped the $, and it is now part of the alias and will run as expected

HTH

Max Friederichs
  • 569
  • 3
  • 13
  • 2
    Thanks, I tried this, but for some reason it didn't work. I looked up escaping the quotes (based on your suggestion) and got it working with: alias logfile='adb logcat -v threadtime *:V | tee /c/Users/ghannan/Desktop/adb-logs/''$(date +%Y-%m-%d@%Hh%Mm%Ss)''-FullLogCat.log' Further information found here: https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash/42082956#42082956 – Gordon H Apr 24 '18 at 19:06
  • The reason the version in this answer doesn't work is that it puts single-quotes around the command expansion (i.e. it has `...'$(date ...)'...`), and single-quotes prevent the command expansion from being run. The version that works has double-quotes around it instead, which allow command and variable expansions. BTW, I'd actually recommend avoiding all the quoting confusion by using a function instead of an alias -- see [chepner's answer here](https://stackoverflow.com/questions/14560796/bash-aliases-and-awk-escaping-of-quotes). – Gordon Davisson Apr 24 '18 at 19:33
  • Or see my answer below :) – chepner Apr 24 '18 at 19:48
  • Yes, the single quotes need to be removed as well.. updated answer to reflect – Max Friederichs Apr 24 '18 at 19:55
1

use single quotes to prevent the command substitution until the alias is actually executed.

alias logfile='adb logcat -v threadtime *:V | tee /c/Users/username/Desktop/adb-logs/$(date +%Y-%m-%d@%Hh%Mm%Ss)-FullLogCat.log'
# ............^................................................................................................................^
glenn jackman
  • 238,783
  • 38
  • 220
  • 352