Being inherently lazy, I am always a sucker for shortcuts, neat tricks to cut my work and, most important, not to do the same thing again and again. Here is a tip I find useful.
Have you ever been frustrated to find that some line has changed some important shell variable such as ORACLE_BASE inside a shell script? The list of variables that are important to safety and efficiency of your shell is a long one - PS1, ORACLE_BASE, PATH, and so on. Using this little known command, you can easily "protect" a variable. The trick is to make it readonly. First, set the variable:
# export ORACLE_BASE=/opt/oracle
Then make it readonly:
# readonly ORACLE_BASE
Now if you want to set it:
# export ORACLE_BASE=/opt/oracle1
-bash: ORACLE_BASE: readonly variable
You can't. You can't even unset the variable:
# unset ORACLE_BASE
-bash: unset: ORACLE_BASE: cannot unset: readonly variable
This is a cool way to protect important variables.
To get a list of variables that are readonly, use
# declare -r
declare -ar BASH_VERSINFO='(="3" ="00" ="15" ="1" ="release" ="i386-redhat-linux-gnu")'
declare -ir EUID="500"
declare -rx ORACLE_BASE="/opt/oracle"
declare -ir PPID="13204"
declare -r SHELLOPTS="braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor"
declare -ir UID="500"
Unfortunately there is no comamnd to make it readwrite.
In the same way, you can also prevent a specific variable not to be set. LD_LIBRARY_PATH should not be set during some type of installations. To force it that way:
# export LD_LIBRARY_PATH
# readonly LD_LIBRARY_PATH
Now if you want to assign a value:
# export LD_LIBRARY_PATH=d
-bash: LD_LIBRARY_PATH: readonly variable
You will not be able to. You can also achieve the same goal by:
# declare -r LD_LIBRARY_PATH=
I hope you find it useful.
Confessions of an Oracle Database Junkie - Arup Nanda The opinions expressed here are mine and mine alone. They may not necessarily reflect that of my employers and customers - both past or present. The comments left by the reviewers are theirs alone and may not reflect my opinion whether implied or not. None of the advice is warranted to be free of errors and ommision. Please use at your own risk and after thorough testing in your environment.
Monday, January 19, 2009
Making a Shell Variable Read Only
Subscribe to: Post Comments (Atom)
Fantastic. I'm a long-time Linux/Unix user, and was unaware of this feature...though arguably I *should* have been aware. ;-) Thanks for pointing it out.
The "readonly" status is effective for the current session only. If you spawn a new bash shell the variable loses it's "readonly" status and can be assigned new value. "readonly export myvar=5" does not export it as "readonly".
I was searching for a way to make a variable readonly in current session as well as in subshells spawned by current shell and come across this post. Just wanted to share my findings and check if anyone has solution for this. Thanks...
To get the list of readonly variables, execute the readonly command by itself or with the "-p", print, option.
you can open any search engine and get to know more about the way of how to repair a visualfoxpro file is corrupted provided by some applications
wow great work for linux,unix user ,this feature is very nice i like it keep sharing thanks i will keep visiting .
When the writers are writing Management Writing Service, they have to ensure they conduct Criminology Writing Service on the topic areas and use only recent sources.
oracle adf training course
oracle rac online training
office 365 online training
I think this article is useful to everyone.
The best top 1 casino website
I think these must be useful to you.
Thank you for your interest.
The best top 1 casino website
Post a Comment