Operating System - HP-UX
1833756 Members
2414 Online
110063 Solutions
New Discussion

Re: Certain properties of shell variables

 
SOLVED
Go to solution
Ralph Grothe
Honored Contributor

Certain properties of shell variables

Hi,

this is just an inferior aside to my "No Login..." thread (see below).

While I was contemplating about the savecrash stuff and looking at the various crash dump related init scripts and configuration files, I came accross this readonly variable declaration.

# grep typeset\ -r /sbin/init.d/savecrash
typeset -r DEFAULT_SAVECRASH_DIR=/var/adm/crash


Well I thought, nice feature of the POSIX shell I haven't made use of so far.
And at the shell I played, what happens if...

Let's see if maybe there is already tacitly being made use of such an esoteric feature?

# typeset -r
#

Relief, this doesn't seem to be standard fare.

So I need to define my own

# typeset -r outch=noli\ me\ tangere
# typeset -r
outch='noli me tangere'

swallowed it.
Isn't it funny how they foster ambiguity or Perl's TIMTOWTDI principle?

# readonly -p
readonly outch='noli me tangere'

# outch=really
sh: outch: This variable is read only.

ah, it's resisting.

But I want to get rid of this damned thingy

# typeset +r outch
sh: outch: This variable is read only.
# undef outch
sh: undef: not found.
# typeset -u -r outch
# readonly -p
readonly outch='NOLI ME TANGERE'

Oops, not what I intended.
It's getting presumptuous.

It even seems to survive accross subshells

# ( typeset -r; unset outch )
outch='NOLI ME TANGERE'
sh: outch: This variable is read only.

Does this mean such declared variables will only fall to demise whith the exit of the shell?

I haven't noticed this feature before and merely am amused.


Madness, thy name is system administration
3 REPLIES 3
Pete Randall
Outstanding Contributor
Solution

Re: Certain properties of shell variables

Ralph,

"Does this mean such declared variables will only fall to demise whith the exit of the shell?"

Yes, that's exactly what it means. This is a useful feature if you want to set system-wide variables (in /etc/profile, for example), that you don't want the user to be able to change.


Pete


Pete
curt larson_1
Honored Contributor

Re: Certain properties of shell variables

a read only variable will give you an error message if you attempt to change the value, turn off its readonly attribute, or unset it.

it can still change value if it is a variable the the shell automatically changes such as PWD

doesn't make much since to have a read only variable that you can modify by just removing it and recreating it as desired.
Ralph Grothe
Honored Contributor

Re: Certain properties of shell variables

Ok, I can see the need for this property.
But then I wouldn't call it variable but rather constant.
Maybe a more obvious, intuitive syntactic wrap on behalf of the shell designers wouldn't have astonished me.
In Perl they have the "use constant" pragma to this end.
Madness, thy name is system administration