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:
Adblock detected 😱
My website is made possible by displaying online advertisements to my visitors. I get it! Ads are annoying but they help keep this website running. It is hard to keep the site running and producing new content when so many people block ads. Please consider donating money to the nixCraft via
PayPal/
Bitcoin, or become a
supporter using Patreon.
Create a shell script called functions.sh as follows:
#!/bin/bash
JAIL=/jaildir
addusertojail(){
echo "Adding users..."
}
setupjail(){
echo "Setting up jail"
} |
#!/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 |
#!/bin/bash
source functions.sh # access $JAIL defined in functions.sh
echo "Jail dir : $JAIL" # call functions
addusertojail
setupjail
Posted by: Vivek Gite
The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.
Thanks,
This if for the first time , I have herd about source command..
very informative post :)
hi, ‘source’ it’s similar to ‘.’.
‘.’ is a synonym for ‘source’
Good Explanation….