Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Can an alias ever have a dynamic element?
- X-seq: zsh-users 18239
- From: Phil Pennock <zsh-workers+phil.pennock@xxxxxxxxxxxx>
- To: zzapper <david@xxxxxxxxxxxxxx>
- Subject: Re: Can an alias ever have a dynamic element?
- Date: Tue, 17 Dec 2013 11:04:15 -0500
- Cc: zsh-users@xxxxxxx
- Dkim-signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=spodhuis.org; s=d201210; h=In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date; bh=1BS54ceawRXo7OYoHmxLTtFtzXqz1GjJlA7ut8ybKfw=; b=C+9AGXag0o1i2OKlZDMRz59qB+yxkUew1weEq4BDYd32QMC8CXnoNTzjGrd57+Hf1PuJFCqFY7lVXyGLX1CPTyxlRlPGFJn9ckEFNblZo8Pw9rzazXRwLreVYpIGBWdRhbljqYcUY2DeN2PE/LEIXObgJ5VXUgwIJhUwq1SrSbQ=;
- In-reply-to: <XnsA2999C087BB98davidrayninfocouk@80.91.229.13>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mail-followup-to: zzapper <david@xxxxxxxxxxxxxx>, zsh-users@xxxxxxx
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- Openpgp: url=https://www.security.spodhuis.org/PGP/keys/0x3903637F.asc
- References: <XnsA2999C087BB98davidrayninfocouk@80.91.229.13>
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
-Phil
Messages sorted by:
Reverse Date,
Date,
Thread,
Author