2

Each morning I run the following command that tails a log file created daily:

ssh -t [email protected] tail -f development/abc/$(date -u +%Y%m%d).log

This works just fine and each morning the log file of that day gets tailed with no issues. This past Friday I created the following alias for this command in .zshrc and on Friday it worked flawlessly:

alias abc_log='ssh -t [email protected] tail -f development/abc/$(date -u +%Y%m%d).log'

This morning I ran abc_log and was returned this error:

    tail: cannot open `development/abc/20160509.log' for reading: No such file or directory
Connection to 55.555.55.55 closed.

Once I ran source ~/.zshrcthen abc_log worked as normal. I prefer not to have to reload .zshrc each morning. Suggestions? Thanks!

2
  • 1
    Are you sure that you're using single quotes? That should work. An alternative would be to write a one-liner function instead of using an alias.
    – djf
    Commented May 9, 2016 at 10:58
  • 2
    20160509 is today's date, so in what way is the command wrong there? Maybe today's log file hadn't been created yet, what time is it created and what time did you run the command? Commented May 9, 2016 at 21:20

1 Answer 1

0

The embedded date command will only be evaluated once: when the alias is created. That's the reason reloading the .zshrc makes it work again. If you use a function instead as djf suggested, it will be evaluated every time which seems to be your intended behavior.

5
  • 1
    It shouldn't. As stated by @djf, if single quotes are used, it should be evaluated everytime the alias is used.
    – user60039
    Commented May 9, 2016 at 12:19
  • It appears so... tested with a fresh install of zsh and. bash has the same behavior. Not sure if this double expansion is very posix compliant... will test with other shells when i get a chance.
    – user157627
    Commented May 9, 2016 at 21:03
  • There is no double expansion. (Not as part of the alias, I mean — the date command is executed when the alias is run, and its output is in turned parsed by the remote shell, but the output is just digits which are interpreted as they are.) Commented May 9, 2016 at 21:21
  • I don't think you are right on the second count. both the alias and the date command are expanded on the local machine. you can test that with something like ssh host echo hostname.
    – user157627
    Commented May 9, 2016 at 23:15
  • I can confirm using zsh on macOS Catalina that alias echo-date="echo $(date)" regularly outputs the same value, whereas a function call like echo-date() { echo "$(date)" } generates unique output each time.
    – ryantuck
    Commented Apr 13, 2021 at 13:54

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