From owner-freebsd-questions@FreeBSD.ORG Wed Jul 15 05:53:10 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 759D41065677 for ; Wed, 15 Jul 2009 05:53:10 +0000 (UTC) (envelope-from dan@dan.emsphone.com) Received: from email2.allantgroup.com (email2.emsphone.com [199.67.51.116]) by mx1.freebsd.org (Postfix) with ESMTP id E2E718FC21 for ; Wed, 15 Jul 2009 05:53:09 +0000 (UTC) (envelope-from dan@dan.emsphone.com) Received: from dan.emsphone.com (dan.emsphone.com [199.67.51.101]) by email2.allantgroup.com (8.14.0/8.14.0) with ESMTP id n6F5r7ji015246 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 15 Jul 2009 00:53:08 -0500 (CDT) (envelope-from dan@dan.emsphone.com) Received: from dan.emsphone.com (smmsp@localhost [127.0.0.1]) by dan.emsphone.com (8.14.3/8.14.3) with ESMTP id n6F5r7n8022550 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 15 Jul 2009 00:53:07 -0500 (CDT) (envelope-from dan@dan.emsphone.com) Received: (from dan@localhost) by dan.emsphone.com (8.14.3/8.14.3/Submit) id n6F5r50C022532; Wed, 15 Jul 2009 00:53:05 -0500 (CDT) (envelope-from dan) Date: Wed, 15 Jul 2009 00:53:05 -0500 From: Dan Nelson To: Bryan Venteicher Message-ID: <20090715055305.GG63413@dan.emsphone.com> References: <4A48C83B-A36C-417F-9F68-F1CB1BCDDC8F@socket.net> <142219524.01247634136492.JavaMail.root@bayleaf> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <142219524.01247634136492.JavaMail.root@bayleaf> X-OS: FreeBSD 7.2-STABLE User-Agent: Mutt/1.5.19 (2009-01-05) X-Virus-Scanned: ClamAV version 0.94.2, clamav-milter version 0.94.2 on email2.allantgroup.com X-Virus-Status: Clean X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-2.0.2 (email2.allantgroup.com [199.67.51.78]); Wed, 15 Jul 2009 00:53:08 -0500 (CDT) X-Scanned-By: MIMEDefang 2.45 Cc: Jay Hall , freebsd-questions@freebsd.org Subject: Re: Bash and arrays X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Jul 2009 05:53:11 -0000 In the last episode (Jul 15), Bryan Venteicher said: > > I thought I understood how arrays work in bash, but I have been proven > > wrong. I am reading lines from a file and placing them in an array. > > However, when I am finished, the array has a length of 0. > > > > Following is the code I am using. > > > > #!/usr/local/bin/bash > > COUNTER=0 > > cat ./test_file.txt | while read LINE > > do > > echo ${LINE} > > FOO[${COUNTER}]=${LINE} > > COUNTER=`expr ${COUNTER} + 1` > > done > > > > echo ${#FOO[@]} > > echo ${#FOO[*]} > > > > > > And, here is the output. > > > > test_file > > file_size > > 0 > > 0 > > > > Thanks in advance for any help you can offer. > > The right hand side of the pipe is running in its own subshell so > it has its own copy of FOO. > > One fix is > #!/usr/local/bin/bash > COUNTER=0 > while read LINE > do > echo ${LINE} > FOO[${COUNTER}]=${LINE} > COUNTER=`expr ${COUNTER} + 1` > done < ./test_file.txt Another alternative would be to use zsh, which makes sure that the last component of a pipeline is run in the current shell process so the original script would have worked. -- Dan Nelson dnelson@allantgroup.com