Wednesday, July 9, 2008

A-Parently Scripture

If you run an executable on a Linux platform which requires shared libraries, you might have to add the relevant paths of shared libraries to the LD_LIBRARY_PATH environment variable. Recently, I had to work on a development board for which I had only the zImage and ramdisk.jffs2 (and not the sources). So everytime I powered-on the board and loaded the Linux operating system I had to set up the LD_LIBRARY_PATH environment variable before I could run some applications that were accessible over a NFS mount. I thought of writing a script which will effect these changes to the environment variable thus saving me the monotony of typing the paths again and again. Even though I was able to execute the script, the desired change didnt happen.

That's when I came to know how the bash shell works. When a shell script is executed from the bash shell prompt, it is spawned as a child process and whatever actions are taken in the script (like changing the environment variable) will affect only the child process's copy of the variables and not the parent process.

I found a tip to overcome this hurdle in the "Linux For You" magazine (May 2008 edition) under the "Tips & Tricks" section. If setpath.sh is the script file containing the command

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/kailashs/bin/

then for this to take effect in the parent shell use the following command

root-bash# source setpath.sh

The source command will read and execute commands from the file and return. All the commands executed now will affect the parent shell.