From owner-freebsd-questions@FreeBSD.ORG Fri Feb 2 14:06:46 2007 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id E6D4B16A406 for ; Fri, 2 Feb 2007 14:06:45 +0000 (UTC) (envelope-from erikt@midgard.homeip.net) Received: from ch-smtp02.sth.basefarm.net (ch-smtp02.sth.basefarm.net [80.76.149.213]) by mx1.freebsd.org (Postfix) with ESMTP id 73CF113C478 for ; Fri, 2 Feb 2007 14:06:45 +0000 (UTC) (envelope-from erikt@midgard.homeip.net) Received: from c83-253-29-241.bredband.comhem.se ([83.253.29.241]:60977 helo=falcon.midgard.homeip.net) by ch-smtp02.sth.basefarm.net with smtp (Exim 4.63) (envelope-from ) id 1HCz3T-00057v-8i for freebsd-questions@freebsd.org; Fri, 02 Feb 2007 15:06:20 +0100 Received: (qmail 45315 invoked from network); 2 Feb 2007 15:06:16 +0100 Received: from owl.midgard.homeip.net (10.1.5.7) by falcon.midgard.homeip.net with SMTP; 2 Feb 2007 15:06:16 +0100 Received: (qmail 10037 invoked by uid 1001); 2 Feb 2007 15:06:16 +0100 Date: Fri, 2 Feb 2007 15:06:16 +0100 From: Erik Trulsson To: J65nko Message-ID: <20070202140616.GA10025@owl.midgard.homeip.net> Mail-Followup-To: J65nko , Tigger , freebsd-questions@freebsd.org References: <20070202111156.GA41151@lilypie.com> <19861fba0702020448u50ac68adqf205a3cd001e901f@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <19861fba0702020448u50ac68adqf205a3cd001e901f@mail.gmail.com> User-Agent: Mutt/1.5.13 (2006-08-11) X-ACL-Warn: Too high rate of unknown addresses received from you X-Scan-Result: No virus found in message 1HCz3T-00057v-8i. X-Scan-Signature: ch-smtp02.sth.basefarm.net 1HCz3T-00057v-8i b89ba2dc63ad81513339a14a823d89c2 Cc: freebsd-questions@freebsd.org, Tigger Subject: Re: unexpected result from sh script with `date` 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: Fri, 02 Feb 2007 14:06:46 -0000 On Fri, Feb 02, 2007 at 01:48:31PM +0100, J65nko wrote: > On 2/2/07, Tigger wrote: > >Hello, the following simply sh script is outputting unexpected results. > >Any idea why? > > > >--script-- > > > >#!/bin/sh > > > >started=`date` > > > >echo "Started at: $started" > >echo "Finished : "`date` > >exit > > > >--output-- > > > >Started at: Fri Feb 2 22:13:51 EST 2007 > >Finished : Fri Feb 2 22:13:51 EST 2007 > > > >--problem-- > > > >Between 'Feb' and '2', there is two spaces on the 'Started at' line, > >however the 'Finished' one only has 1 space. > > > >I know this sounds picky, but I was not expecting this at all. > > > >uname -a > >FreeBSD piglet 6.2-STABLE FreeBSD 6.2-STABLE #0: Fri Jan 19 04:13:20 EST > >2007 tigger@piglet:/usr/obj/usr/src/sys/PIGLET i386 > > The same on OpenBSD here (ksh) > OpenBSD 4.0-current (GENERIC) #1194: Thu Nov 2 16:32:12 MST 2006 > deraadt@i386.openbsd.org:/usr/src/sys/arch/i386/compile/GENERIC > > It seems to depend whether the command substitution is within the > quote-delimited string, for 'echo' or outside that string, in other > words on its own. > ------- script---------- > #!/bin/sh > > started=$(date) > > echo "\$started within \" delimited string for echo" > echo "Started at: $started" > echo "Command substitution \$(date) within \" delimited string for echo" > echo "Finished : $(date)" > echo "Command substitution \$(date) outside \" delimited string for echo" > echo "Finished : "$(date) > echo "Command substitution \`date\` outside \" delimited string for echo" > echo "Finished : "$(date) > ----------------------------------- > Output: > ------------------- > $started within " delimited string for echo > Started at: Fri Feb 2 13:46:07 CET 2007 > Command substitution $(date) within " delimited string for echo > Finished : Fri Feb 2 13:46:07 CET 2007 > Command substitution $(date) outside " delimited string for echo > Finished : Fri Feb 2 13:46:07 CET 2007 > Command substitution `date` outside " delimited string for echo > Finished : Fri Feb 2 13:46:07 CET 2007 > --------------------------------------- > Embedded inside the string there are two spaces between Feb and the 2, > as "stand-alone" there is only one space. > > Strange indeed ;) Not strange at all if think about how 'echo' works and how the arguments are passed to it. If you do a echo `date` then echo will be passed 6 arguments ('Fri' 'Feb' '2' '13:46:07' 'CET' '2007') and print them with a single space between each of them. If you do a echo "`date`" then echo will be passed only a single argument ('Fri Feb 2 13:46:07 CET 2007') which will be printed unmodified. As another example compare the outputs of echo a b c and echo "a b c" -- Erik Trulsson ertr1013@student.uu.se