From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3502 invoked by alias); 7 Jul 2014 21:33:13 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 18931 Received: (qmail 13888 invoked from network); 7 Jul 2014 21:33:11 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,HTML_MESSAGE, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=nFtRUmRSXRkBX37eTzdjOwFgsZzQ/Bjoc/369BMXBbo=; b=ZWEK0CciHPAcuL0j04ZQRpOpKtiN6pEiyylszQjF5okVMOUNBN3VcLb/Xyeq1YdbzD dGabYmjqohxpCNjmyP/KdNcwwq/+mDJTi481RcTqcUylMnflDFTSM9qZl2muQLpmdUUR xu2k2iNAvEWqYlyxgdx13VoddewaPS/e91wy4OLu8k8HcAn79qlyzx9SUY0Lyas1DNE4 W6o5HCqk+8ayUStEksPhZOXZuOJaS3sAd7AKcRzv08drWTE0u7WgbalyjhRO7rlbU9WR TYyGdoOPOAXFj0O1bKl2Wvd0AeG/gk3HHwAVQehZ2s8MXZeM9vvqHtD0b4k7XZm3lqj4 yKWw== X-Gm-Message-State: ALoCoQnrgvYSlQtUPYt+60tThLOE0PWK04meBpW6/r+0B5gLA5oxNVO2x1Zt0wHqgpiWRJyoE9j2 MIME-Version: 1.0 X-Received: by 10.220.203.134 with SMTP id fi6mr30068761vcb.18.1404768785979; Mon, 07 Jul 2014 14:33:05 -0700 (PDT) In-Reply-To: References: Date: Mon, 7 Jul 2014 14:33:05 -0700 Message-ID: Subject: Re: how do I get the last argument from a list of arguments? From: Kurtis Rader To: TJ Luoma Cc: Zsh-Users List Content-Type: multipart/alternative; boundary=001a11c1bbc07af6bc04fda13833 --001a11c1bbc07af6bc04fda13833 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable See the description of the *${name:offset} *syntax in *man zshexpn*. Note the space between the colon and minus-sign is required to disambiguate it from the "var:-default" syntax. function lastarg() { print "${@[-1]}" print "${@: -1}" } lastarg 1 2 3 On Mon, Jul 7, 2014 at 2:06 PM, TJ Luoma wrote: > I=E2=80=99m trying to learn better ways of dealing with arguments given t= o a > function, because I am sure that I am not doing it the most efficient > way. > > For example, if I want to process a series of args, I usually use a > loop like this: > > for FOO in "$@" > do > case "$FOO" in > -t|--to) > shift > TO=3D"$1" > shift > ;; > > -v|--verbose) > VERBOSE=3D'yes' > shift > ;; > > -*|--*) > echo " $NAME [warning]: Don't know what to do with arg: $1= " > shift > ;; > > esac > > done # for args > > That has worked OK for what I've needed to do, but now I'm trying to > create two functions which I will use in place of 'cp' and 'mv' and I > need to be able to find the _last_ argument (the destination) before I > process all the rest of the args. > > The only way that I can think of to get the last argument is to do > something like this > > LAST=3D`echo "$@" | awk '{print $NF}'` > > but that made me wonder if there wasn=E2=80=99t a better way. > > Note that this does not need to be 'portable' at all -- I will happily > use any zsh-specific features which may exist, as long as it works in > 5.0.2 (which is what comes with OS X). > > Thanks! > > TjL > --=20 Kurtis Rader Caretaker of the exceptional canines Junior and Hank --001a11c1bbc07af6bc04fda13833--