Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Can an alias ever have a dynamic element?

On 2013-12-17 at 15:20 +0000, zzapper wrote:
> I have an alias which creates a tar with a date component in its name that 
> date is however evaluated when the shell is created which could have been a 
> few days ago. The easy solution is to create a script but is they any 
> alternative?

Use a function.

There are a very few use-cases for aliases, which mostly boil down to
"need to mess with how zsh parses the command-line"; otherwise, you
should use functions.  Use functions unless and until you know you need
an alias.

"alias -g" for in-place mutations, or plain aliases which tinker with
state in some other way, but otherwise, use functions.

    function tar_with_initial_timestamp {
      zmodload -i zsh/datetime || return 1
      local invoked_s invoked_rendered
      invoked_s=$(( EPOCHSECONDS - SECONDS ))
      TZ="" strftime -s invoked_rendered "%Y%m%d_%H%M%SZ" $invoked_s
      tar cvf "capture_$invoked_rendered.tar" "$@"

    tar_with_initial_timestamp dir1 dir2 metadata.json


Messages sorted by: Reverse Date, Date, Thread, Author