Monday, January 19, 2009

Making a Shell Variable Read Only

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='([0]="3" [1]="00" [2]="15" [3]="1" [4]="release" [5]="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.

8 comments:

Anonymous said...

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.

Regards,

John P.

Anonymous said...

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...

Logan P said...

To get the list of readonly variables, execute the readonly command by itself or with the "-p", print, option.

$readonly


$readonly -p

Anonymous said...

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

Robert said...

wow great work for linux,unix user ,this feature is very nice i like it keep sharing thanks i will keep visiting .

bossanova.1123@gmail.com said...

The best top 1 casino website
กติกาเสือมังกร เกมไพ่สายโหด

bossanova.1123@gmail.com said...

The best top 1 casino website
วิธีเล่นเสือมังกร ให้ได้เงิน2022

pg slot said...

สล็อต PGSLOTแตกง่าย เล่นง่ายได้เงินจริงผู้ให้บริการเกมสล็อต pg slot ออนไลน์บนโทรศัพท์เคลื่อนที่ที่มีเกมนานาประการให้เลือก เป็นเกมรูปแบบใหม่ที่ทำเงินให้ผู้เล่นได้เงินจริง 

Translate