On 01/04/2011 08:05 AM, Eric Blake wrote:
> I couldn't find anything either - the POSIX wording for readonly only
> mentions assignment and unset as requiring errors.  I think that's an
> unintentional hole in POSIX, though, so I'm going ahead and submitting a
> bug report to have readonly also mention read and getopts as being
> required to error out on a readonly variable (and given that ksh treats
> assignment different than unset on whether a non-interactive shell
> exits, the extent of the reaction for getopts and read will probably
> have to allow both behaviors).

I found some other differences between shells:

$ bash --posix -c 'cd /tmp; readonly PWD; echo $?; cd ..; echo
$?-$PWD-$(pwd)' || echo abort,$?
0
bash: PWD: readonly variable
0-/tmp-/

$ bash -c 'cd /tmp; readonly PWD; echo $?; cd ..; echo $?-$PWD-$(pwd)'
|| echo abort,$?
0
bash: PWD: readonly variable
0-/tmp-/

$ ksh -c 'cd /tmp; readonly PWD; echo $?; cd ..; echo $?-$PWD-$(pwd)' ||
echo abort,$?
0
0-/-/

Bash goes ahead and changes the directory but leaves PWD untouched (PWD
is now inconsistent without warning!) in both posix and bash mode,
whereas ksh (silently) ignores the request to make PWD readonly in the
first place.

Also, both shells abort a non-interactive shell when readonly interferes
with export (but bash only aborts in posix mode):

$ ksh -c 'readonly v; export v=a; echo $?-$a' || echo abort,$?
ksh: line 1: v: is read only
abort,1

$ bash -c 'readonly v; export v=a; echo $?-$a' || echo abort,$?
bash: v: readonly variable
1-

$ bash --posix -c 'readonly v; export v=a; echo $?-$a' || echo abort,$?
bash: v: readonly variable
abort,1

I've gone ahead and filed a POSIX interpretation request:
http://austingroupbugs.net/view.php?id=367

Also, since the next version of POSIX will be mandating changes for cd
(http://austingroupbugs.net/view.php?id=253 adds the new cd -e option to
warn if PWD is inconsistent), the notion of a readonly PWD may affect
how you implement that proposal.

-- 
Eric Blake   ebl...@redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to