SHELLOPTS=igncr and bash --posix

Dave Korn dave.korn.cygwin@gmail.com
Fri Dec 30 19:52:00 GMT 2011


On 30/12/2011 12:41, Rafael Kitover wrote:
> On 12/30/2011 1:06 AM, Dave Korn wrote:
>> On 29/12/2011 21:40, Rafael Kitover wrote:
>>> Some of my bash scripts, particularly ones that are #!/bin/bash --posix
>>> trigger a nasty warning when I have SHELLOPTS=igncr set in my ~/.zshrc:
>>>
>>> rkitover@eeebox ~/src/scala % echo $SHELLOPTS
>>> igncr
>>> rkitover@eeebox ~/src/scala % bash --posix
>>> bash: SHELLOPTS: readonly variable
>>>
>>> this is rather unsightly so I have turned off SHELLOPTS for now.
>>>
>>> Is this a bug? 
>>   Bug in your script I'm afraid.  SHELLOPTS is indeed a readonly variable, it
>> can only be set outside bash before starting it, definitely not from within a
>> startup script.  (Perhaps the reason it seems associated with --posix is
>> because that selects different startup files?  I'm guessing that you may have
>> ENV=~/.zshrc because it's not normal behaviour for bash to read zsh's startup
>> file!)
> 
> I don't have ENV set.
> 
> If I comment out the SHELLOPTS setting in ~/.zshrc then do this the
> problem still appears:
> 
> rkitover@eeebox ~ % export SHELLOPTS=igncr
> rkitover@eeebox ~ % bash --posix
> bash: SHELLOPTS: readonly variable
> %{
> %}(%n@%m)[%h] %{%}%~%{%}
> $
> 
> I do not have a ~/.profile, and ~/.bash_profile just reads ~/.bashrc. I
> do not set SHELLOPTS in ~/.bashrc, it only has set -o igncr.

  Ah, I'm guilty of trusting the documentation too much.  Turns out the
problem is easy to reproduce without zsh being anything to do with it: even if
you have SHELLOPTS set in the Windows environment and launch bash directly
from cmd.exe, it still fails -

> Microsoft Windows 2000 [Version 5.00.2195]
> (C) Copyright 1985-2000 Microsoft Corp.
> 
> C:\Documents and Settings\Administrator>set | grep SHELLOPT
> SHELLOPTS=igncr
> 
> C:\Documents and Settings\Administrator>bash --posix
> bash: SHELLOPTS: readonly variable
> bash-4.1$

  A bit of googling and I discovered this is apparently a known bug in
upstream bash, reported on the GNU bug-bash list back in October at
http://lists.gnu.org/archive/html/bug-bash/2011-10/msg00039.html.  A fix is
promised for the next release but until then I'm afraid you're stuck with only
being able to use one of SHELLOPTS and --posix at a time.

>>> If so, is a fix possible?
>>
>> Use "set -o igncr" instead.
>
> But if I want to use CRLF #/bin/sh scripts from zsh, then I have to set
> SHELLOPTS.

  You might be able to make it work by setting ENV (would be BASH_ENV, but
you're using --posix) to point to a (LF-endings-only) script that contains
"set -o igncr".  Hopefully that will take effect before the newly-launched
bash has a chance to get upset by any CRs in the script file it's been invoked
on.  (I can't guarantee that nothing will go wrong while parsing the shebang
line if that ends with a CRLF, but you may as well give it a try.)

    cheers,
      DaveK


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple



More information about the Cygwin mailing list