≡ Menu

Linux / UNIX source Command Examples

How do I use source command under UNIX / Linux / BSD / OS X operating systems?

The source command in shell is used to execute commands from a file in the current shell. This is useful to load function or variables stored in another file.

source command syntax

The syntax is as follows:

source filename
source /path/to/file

It will read and execute commands from given FILENAME in the current shell. The entries in $PATH are used to find the directory containing FILENAME.

source command Examples

Create a function file called functions.sh as follows:

hello(){
  echo "I'm hello()"
}    
getos(){
  echo "OS is - $(uname)"
}

Create a shell script called test.sh as follows

#!/bin/bash
source functions.sh
# call hello function
hello
# call getos function
getos

Save and close the file. Run it as follows:
chmod +x test.sh
./test.sh

See also:

Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:



{ 0 comments… add one }
Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">


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