I recently discovered that it is possible to set attributes on readonly
variables

  $ declare -p PPID
  declare -ir PPID="759437"
  $ declare -l PPID
  $ declare -p PPID
  declare -irl PPID="759437"

I noticed SHELLOPTS and BASHOPTS among the default readonly variables
set by bash.
They are modified by bash whenever you set a set option or shopt.

I tried setting the -i attribute on them, and got this result:

  $ declare -i SHELLOPTS
  $ set -f
  bash: set: 
braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor:noglob:
 syntax error in expression (error token is 
":emacs:hashall:histexpand:history:interactive-comments:monitor:noglob")
  $ declare -p SHELLOPTS
  declare -i 
SHELLOPTS="braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor"
  $ set -C
  bash: set: 
braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor:noclobber:noglob:
 syntax error in expression (error token is 
":emacs:hashall:histexpand:history:interactive-comments:monitor:noclobber:noglob")
  $ declare -p SHELLOPTS
  declare -i 
SHELLOPTS="braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor"
  $ SHELLOPTS=123; declare -p SHELLOPTS
  declare -i SHELLOPTS="123"
  $ set +B
  
emacs:hashall:histexpand:history:interactive-comments:monitor:noclobber:noglob: 
syntax error in expression (error token is 
":hashall:histexpand:history:interactive-comments:monitor:noclobber:noglob")
  $ declare -p SHELLOPTS
  declare -i SHELLOPTS="123"
  $ declare +i SHELLOPTS=hello; declare -p SHELLOPTS
  declare -- SHELLOPTS="hello"
  $ set -o posix
  $ declare -p SHELLOPTS
  declare -r 
SHELLOPTS="emacs:hashall:histexpand:history:interactive-comments:monitor:noclobber:noglob:posix"

The arithmetic syntax error caused by trying to assign the new list of
options to SHELLOPTS prevents bash from changing its value, and
re-adding the readonly attribute to SHELLOPTS.

Then SHELLOPTS can be modified, and unset freely. :-)

The same is possible with BASHOPTS.

o/
 emanuele6

Reply via email to