From owner-freebsd-questions@FreeBSD.ORG Mon Mar 14 02:05:09 2011 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 8D0CE1065670 for ; Mon, 14 Mar 2011 02:05:09 +0000 (UTC) (envelope-from max@mxcrypt.com) Received: from mail-vx0-f182.google.com (mail-vx0-f182.google.com [209.85.220.182]) by mx1.freebsd.org (Postfix) with ESMTP id 4E26B8FC0C for ; Mon, 14 Mar 2011 02:05:09 +0000 (UTC) Received: by vxc34 with SMTP id 34so4535893vxc.13 for ; Sun, 13 Mar 2011 19:05:08 -0700 (PDT) Received: by 10.52.69.79 with SMTP id c15mr60624vdu.249.1300068308554; Sun, 13 Mar 2011 19:05:08 -0700 (PDT) MIME-Version: 1.0 Received: by 10.220.180.135 with HTTP; Sun, 13 Mar 2011 19:04:38 -0700 (PDT) In-Reply-To: References: From: Maxim Khitrov Date: Sun, 13 Mar 2011 22:04:38 -0400 Message-ID: To: Scott Ballantyne Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: FreeBSD Subject: Re: Shell script termination with exit function in backquotes 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: Mon, 14 Mar 2011 02:05:09 -0000 On Sun, Mar 13, 2011 at 9:59 PM, Scott Ballantyne wrot= e: > On Sun, Mar 13, 2011 at 8:26 PM, Maxim Khitrov wrote: >> >> Hello everyone, >> >> I might be doing something dumb here, but this doesn't make sense to >> me. When I run the following script, I would expect to see no output: >> >> ---- >> #!/bin/sh >> >> exit_prog() >> { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0echo -n 'before' >> =C2=A0 =C2=A0 =C2=A0 =C2=A0exit 0 >> =C2=A0 =C2=A0 =C2=A0 =C2=A0echo -n 'after' >> } >> >> echo line 1: `exit_prog` >> echo line 2: >> echo line 3: `exit 1` >> echo line 4: >> ---- >> >> The reason I expect to see no output is because 'exit 0' should be >> called before any of the echo lines are allowed to execute. Instead, >> what I get on FreeBSD 7 & 8 is: >> >> ---- >> line 1: before >> line 2: >> ---- >> >> I don't understand this because 'exit 0' seems to terminate the call >> to 'exit_prog', but the execution of the script continues. However, >> when 'exit 1' is called, the script terminates before printing out the >> last 2 lines. >> >> It seems that 'exit' inside a function doesn't work when that function >> is called with backquotes. I assume it has something to do with the >> fact that commands in backquotes are executed in a sub-shell, but the >> behavior is inconsistent. >> >> When I run the same script on RHEL using bash, all 4 lines are printed: >> >> ---- >> line 1: before >> line 2: >> line 3: >> line 4: >> ---- >> >> What's going on here? >> >> - Max > > Backquotes run the process in a sub process, that exits. and the original > process continues. > Yes, my original expectation of having no output is incorrect, but `exit 1` terminates the parent shell in FreeBSD. That's the source of my confusion. - Max