what is "sourcing" in unix/linux shell..

Let me explore “sourcing” in a UNIX/LINUX shell by few words.

I have seen the usage of “sourcing” for a long time in my linux systems.

Today I thought of making it familiar to my friends by this blog..

[humble@humble ]$ cat /etc/rc5.d/S07iscsid |head -n 30|grep -v “#”

. /etc/rc.d/init.d/functions


[humble@humble ]$

==> Above shows content of “iscsi” service startup script. Most of the init scripts will have some entry like “. /etc/rc.d/init.d/functions”..

What is it?

By this way it is sourcing “/etc/rc.d/init.d/functions” to this corresponding shell script..

So what advantage it brings?

Let us have a look at this file ..

[humble@humble git_text]$ cat /etc/rc.d/init.d/functions|head -n 18
# -*-Shell-script-*-
# functions This file contains functions to be used by most or all
# shell scripts in the /etc/init.d directory.



# Make sure umask is sane
umask 022

# Set up a default search path.
export PATH

# Get a sane screen width
[ -z “${COLUMNS:-}” ] && COLUMNS=80

[humble@humble git_text]$

the “comment” session gives you enough data on why you need to source it in other init scripts.

By sourcing in a shell we are embedding all the variables/functions..etc from this sourced file.. That being said , in some way we can say it is inheriting all the data of “sourced” file , here /etc/rc.d/init.d/functions.

It makes easy to substitute “some of the variables and functions” which are needed by the scripts to get it work.. By placing all of these variables in one destination, it is easy to make changes and it avoid rewriting of the same in all the required places.

When-ever you perform sourcing in a shell , it will not spawn a new shell for it , instead it embed everything in the current shell…

In simple words the “shebang” ( #!/bin/bash) does not have any effect when you perform this .. So new shell is not spawned for its operation. Also you are provided with all the data of sourced file in current shell..

hmmm.. U got something on this and it is enough to start with :)…

Leave a Reply

Your email address will not be published. Required fields are marked *