≡ Menu

UNIX Source Command: Read And Execute Commands From File

What is the use of UNIX source command under bash or any other shell? How do I use source command under UNIX / OS X / Linux operating systems?

Bash shell comes with source command which is used to read and execute commands from given FILENAME and return. If any ARGUMENTS are supplied, they become the positional parameters when FILENAME is executed. This is useful to create your own shell scripting libraries and call them using the source command. The syntax is as follows:

source /path/to/fileName

Create a shell script called functions.sh as follows:

   echo "Adding users..."
   echo "Setting up jail"

Now create a file called test.sh:

source functions.sh
# access $JAIL defined in functions.sh
echo "Jail dir : $JAIL"
# call functions

{ 3 comments… add one }

  • Vamsi August 10, 2010, 1:57 pm

    This if for the first time , I have herd about source command..
    very informative post :)

  • JackS August 24, 2010, 9:17 am

    hi, ‘source’ it’s similar to ‘.’.

    ‘.’ is a synonym for ‘source’

  • Vikram Singh May 30, 2011, 5:12 am

    Good Explanation….

Leave a Comment

   Tagged with: , , , , , , , , , , ,