[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: read builtin and readonly variables
From: |
Eric Blake |
Subject: |
Re: read builtin and readonly variables |
Date: |
Thu, 06 Jan 2011 16:39:27 -0700 |
User-agent: |
Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101209 Fedora/3.1.7-0.35.b3pre.fc14 Lightning/1.0b3pre Mnenhy/0.8.3 Thunderbird/3.1.7 |
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 eblake@redhat.com +1-801-349-2682
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature