UNIX Source Command: Read And Execute Commands From File

by Vivek Gite on August 8, 2010 · 3 comments

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:

#!/bin/bash
JAIL=/jaildir
 
addusertojail(){
   echo "Adding users..."
}
 
setupjail(){
   echo "Setting up jail"
}
 

Now create a file called test.sh:

#!/bin/bash
source functions.sh
 
# access $JAIL defined in functions.sh
echo "Jail dir : $JAIL"
 
# call functions
addusertojail
setupjail

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 3 comments… read them below or add one }

1 Vamsi August 10, 2010

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

Reply

2 JackS August 24, 2010

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

‘.’ is a synonym for ‘source’

Reply

3 Vikram Singh May 30, 2011

Good Explanation….

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 3 + 5 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: