help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Is `readonly` too strict?


From: Greg Wooledge
Subject: Re: [Help-bash] Is `readonly` too strict?
Date: Thu, 22 Feb 2018 08:31:16 -0500
User-agent: NeoMutt/20170113 (1.7.2)

On Thu, Feb 22, 2018 at 12:58:09AM -0600, Peng Yu wrote:
> So I think that there should not be an error message. Is
> `readonly` interpreted too strictly in bash?

readonly is a large hammer that dumb sysadmins use to try to set up
a "restricted shell" environment.  (You may laugh now.)

The use of readonly in a script means you are attempting to use a
large hammer to beat the variable into submission.  Any attempt
to modify the variable IN ANY CONTEXT, PERIOD, will be met with the
hammer.

> $ cat main.sh
> #!/usr/bin/env bash
> # vim: set noexpandtab tabstop=2:
> 
> set -v
> readonly x
> x=abc ./script.sh

You are attempting to modify the value of x in the temporary execution
environment that is constructed for running the child program.  But x
is already marked as readonly.  So you can't modify it.  HAMMER TIME.

Want to cheat the hammer?  Try it this way:

  env x=abc ./script.sh

Now continue laughing at how dumb you have to be to try to set up a
"restricted shell" environment.

Meanwhile, don't use readonly in your scripts.  It's dumb.



reply via email to

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