From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23273 invoked from network); 8 May 2006 08:08:05 -0000 X-Spam-Checker-Version: SpamAssassin 3.1.1 (2006-03-10) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.5 required=5.0 tests=AWL,BAYES_00, FORGED_RCVD_HELO autolearn=ham version=3.1.1 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 8 May 2006 08:08:05 -0000 Received: (qmail 25692 invoked from network); 8 May 2006 08:07:58 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 8 May 2006 08:07:58 -0000 Received: (qmail 10614 invoked by alias); 8 May 2006 08:07:50 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 10227 Received: (qmail 10604 invoked from network); 8 May 2006 08:07:50 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 8 May 2006 08:07:50 -0000 Received: (qmail 24470 invoked from network); 8 May 2006 08:07:50 -0000 Received: from smtpout0151.sc1.cp.net (HELO n082.sc1.cp.net) (64.97.136.151) by a.mx.sunsite.dk with SMTP; 8 May 2006 08:07:49 -0000 Received: from sc (82.26.172.199) by n082.sc1.cp.net (7.2.069.1) id 445C31AA00023BB8 for zsh-users@sunsite.dk; Mon, 8 May 2006 08:07:47 +0000 Received: from chazelas by sc with local (Exim 3.36 #1 (Debian)) id 1Fd0mQ-0001X1-00 for ; Mon, 08 May 2006 09:07:46 +0100 Date: Mon, 8 May 2006 09:07:46 +0100 From: Stephane Chazelas To: zsh-users Mailinglist Subject: Re: substring extraction Message-ID: <20060508080745.GA5371@sc> Mail-Followup-To: zsh-users Mailinglist References: <6F0CB04509C11D46A54232E852E390AC0155A876@MCHP7R6A.ww002.siemens.net> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <6F0CB04509C11D46A54232E852E390AC0155A876@MCHP7R6A.ww002.siemens.net> User-Agent: Mutt/1.5.6i Sender: Stephane Chazelas On Mon, May 08, 2006 at 09:32:50AM +0200, Com MN PG P E B Consultant 3 wrote: > bash has the nice feature to extract a substring from a shell variable. > For example > > # bash example > var=abcd > echo ${var:1:2} # Start at offset 1, returns 2 characters > > displays > > bc That's a ksh feature borrowed by bash. And as of many ksh features, it's got its flaws. That one conflicts with the ${var:-default-value} Bourne operator, so that you need to do ${var: -1} (add a space) to get the last char. > Is there an easy way to achieve a similar effect in zsh, without > reverting to external > tools such as awk or sed? [...] In zsh: $var[2,3] zsh can do it because arrays and scalar variables are two different types. In zsh, $var[2,5] gives the 2nd to 5th element when $var is of array type, and 2nd to 5th char when $var is scalar. In ksh/bash all variables are arrays and $var is a shortcut for ${var[0]} (though it's not completely true of bash). -- Stéphane