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

Re: Function or Alias - Does it matter?

TJ Luoma wrote:
> function if they do the same thing?
> For example, these are (as far as I know) functionally identical:
>          function timestamp_r { strftime "%Y/%m/%d at %-I:%M:%S %p"
>          alias timestamp_r='strftime "%Y/%m/%d at %-I:%M:%S %p" "$EPOCHSECONDS"'
> Is there any reason to prefer one or the other?

If you find your function finishes by passing "$@" as the last argument
to whatever command it is, then there's a good chance that an alias will
suffice. Note that (unless you unsetopt completealiases), completion
after an alias will look at the alias definition so with, e.g:
  alias ll='ls -lFb'
  ll -<tab>
this will still complete options to ls. With the function equivalent that
won't work without an explicit compdef.

Functions can be more flexible with their arguments. For example, the
following slight modification to your function makes EPOCHSECONDS
merely a default if you don't specify a different time. This is not
possible with an alias:
  function timestamp_r { strftime "%Y/%m/%d at %-I:%M:%S %p" "${1:-$EPOCHSECONDS}" }


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