bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: set -ue vs. (),{}


From: Al Elgert
Subject: Re: set -ue vs. (),{}
Date: Mon, 11 Aug 2003 16:47:09 +0200
User-agent: Mutt/1.5.2i

Andy Goth schrieb am 07.08.2003 um 17:30:56 (-0500):
> On Thursday, August 7, 2003 9:18 am, Al Elgert wrote:
> > Maybe this might be a confusion between the behaviour of () and {}
> >
> >     ()      opens a subshell and therefore the unset
> >             variable $a terminates the subshell - not the shell
> >
> >     {}      Is a compound command.
> >             It does not open a subshell and terminates the shell.
> 
> This is POSIX behavior, correct?  I haven't a clue where to check the 
> relevant 
> standards documents... could you give me a pointer?

I do not know if this is POSIX behaviour.

man bash:
   Compound Commands
       A compound command is one of the following:

       (list) list  is  executed  in  a  subshell.   Variable  assignments and
              builtin commands that affect  the  shell's  environment  do  not
              remain in effect after the command completes.  The return status
              is the exit status of list.

       { list; }
              list is simply executed in the current shell environment.   list
              must  be  terminated with a newline or semicolon.  This is known
              as a group command.  The return status is  the  exit  status  of
              list.   Note  that unlike the metacharacters ( and , { and } are
              reserved words and must occur where a reserved word is permitted
              to  be  recognized.   Since they do not cause a word break, they
              must be separated from list by whitespace.

> The reason I ask is that I have a couple shell scripts with code like the 
> following:
> 
> test -d package || ( echo 'Wrong working directory.'; exit 1 )
> 
> to make the script bail with an error if the "package" directory doesn't 
> exist.  But of course only the subshell exists, and the script erroneously 
> continues only to fail and fail again, leaving a smoldering path of 
> destruction in its wake.
> 
> When I first saw said scripts I assumed they were written with plain-jane sh 
> in mind and that this subshell business must be a GNU bash quirk, but I 
> seriously doubt it.

The Bourne Shell and ksh works in an analogous manner.

man solaris-bourne-shell:
     (list)
           Execute list in a sub-shell.

     { list;}
           list is executed in  the  current  (that  is,  parent)
           shell. The { must be followed by a space.

command line:
        1 ultra18 ~>sh
        $? \h \w>( echo message; exit 1; )
        message
        $? \h \w>{ echo message; exit 1; }
        message
        1 ultra18 ~>



man ksh:
     (list)
           Execute list in a separate environment.  Note, that if
           two  adjacent open parentheses are needed for nesting,
           a space must be inserted to avoid  arithmetic  evalua-
           tion as described below.

     {list}
           list is simply executed.  Note that unlike  the  meta-
           characters  (  and  ),  { and } are reserved words and
           must occur at the beginning of a line or after a ;  in
           order to be recognized.

command line:
        1 ultra18 ~>ksh
        0 h w>( echo message; exit 1; )
        message
        1 h w>{ echo message; exit 1; }
        message

greetings,
        Alexander

-- 
Alexander Elgert
Public Gruppe
RechnerBetriebsGruppe    TU Darmstadt  (FB 20)   Tel:  +49 06151 16-4333
                                                 Raum: S1/13 11a (alt 25/11a)

FAQ2: http://www.informatik.tu-darmstadt.de/RBG/service/FAQ2/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]