bug-bash
[Top][All Lists]
Advanced

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

Re: exported SHELLOPTS gives error message from sh


From: Chet Ramey
Subject: Re: exported SHELLOPTS gives error message from sh
Date: Tue, 11 Oct 2011 19:58:01 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1

On 10/6/11 2:54 PM, Len Giambrone wrote:

> Bash Version: 4.1
> Patch Level: 10
> Release Status: release
> 
> Description:
> The man page states:
> 
>        SHELLOPTS
>               A  colon-separated  list of enabled shell options.  Each word in
>               the list is a valid argument  for  the  -o  option  to  the  set
>               builtin command (see SHELL BUILTIN COMMANDS below).  The options
>               appearing in SHELLOPTS are those reported as on by set  -o.   If
>               this  variable  is  in the environment when bash starts up, each
>               shell option in the list will  be  enabled  before  reading  any
>               startup files.  This variable is read-only.
> 
> This works great if I use only bash:
> 
> $ bash --version
> GNU bash, version 4.1.10(2)-release (i386-apple-darwin10.8.0)
> Copyright (C) 2009 Free Software Foundation, Inc.
> License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
> 
> $ cat foo
> echo foo
> ./bar
> $ cat bar
> set -o | grep noclobber
> $ set -o noclobber
> $ export SHELLOPTS
> 
> $ bash foo
> foo
> noclobber            on
> 
> But it doesn't work if I use sh instead:
> 
> $ sh --version
> GNU bash, version 4.1.10(2)-release (i386-apple-darwin10.8.0)
> Copyright (C) 2009 Free Software Foundation, Inc.
> License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
> 
> $ sh foo
> foo
> sh: SHELLOPTS: readonly variable
> noclobber            off
> 
> It also outputs an annoying error message that is cluttering our logfiles.

This is longstanding bash behavior (I went back as far as bash-3.0).  The
issue is that posix mode is enabled very early on in main(), as soon as
the shell recognizes argv[0] == "sh", and that is reflected in SHELLOPTS.
A side effect of manipulating SHELLOPTS is that the variable ends up as
readonly (as the documentation specifies).  With this combination of
posix mode and an exported SHELLOPTS, the variable ends up being made
readonly before the environment is initialized.  I think that the
environment variable should override any value bash has internally, as
long as `posix' is present as appropriate, so the next version of bash
will work around this while initializing variables from the environment.

In the meantime, if the set of options you want to use is fairly contant,
you can use a startup file to set them, and any other variables or options
you want.  That would require changes to your scripts, though.

Greg is right, though.  Bash behaves differently when called as sh, by
design.  Posix mode is not the default bash mode, and assuming it is
is bound to get you at some point.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/



reply via email to

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