source . ./script

source is a synonym for dot/period '.' in bash, but not in POSIX sh, so for maximum compatibility use the period.


When a script is run using source it runs within the existing shell, any variables created or modified by the script will remain available after the script completes. In contrast if the script is run just as filename, then a separate subshell (with a completely separate set of variables) would be spawned to run the script.


ubu1404@ubuntu1404lb01:~$ bash script 
Thu Mar 8 08:02:09 PST 2018
ubu1404@ubuntu1404lb01:~$ source script 
bash: source: /usr/bin/script: cannot execute binary file
ubu1404@ubuntu1404lb01:~$ script
Script started, file is typescript
ubu1404@ubuntu1404lb01:~$ mv script scr
ubu1404@ubuntu1404lb01:~$ bash scr
Thu Mar 8 08:02:47 PST 2018
ubu1404@ubuntu1404lb01:~$ source scr
Thu Mar 8 08:02:51 PST 2018
ubu1404@ubuntu1404lb01:~$ ./scr
Thu Mar 8 08:02:56 PST 2018
ubu1404@ubuntu1404lb01:~$ . ./scr
Thu Mar 8 08:03:00 PST 2018

ubu1404@ubuntu1404lb01:~$ 



  • The source command can be used to load any functions file into the current shell script or a command prompt.
  • It read and execute commands from given FILENAME and return.
  • The pathnames in $PATH are used to find the directory containing FILENAME. If any ARGUMENTS are supplied, they become the positional parameters when FILENAME is executed.

Syntax

  • The syntax is as follows:
source filename [arguments]
source functions.sh
source /path/to/functions.sh arg1 arg2
source functions.sh WWWROOT=/apache.jail PHPROOT=/fastcgi.php_jail

Example

Create a shell script called mylib.sh as follows:
#!/bin/bash
JAIL_ROOT=/www/httpd
is_root(){
   [ $(id -u) -eq 0 ] && return $TRUE || return $FALSE
}
Save and close the file. You can now call and use function is_root() from mylib.sh using the following syntax in your script called test.sh:
#!/bin/bash
# Load the  mylib.sh using source comamnd
source mylib.sh

echo "JAIL_ROOT is set to $JAIL_ROOT"

# Invoke the is_root() and show message to user
is_root && echo "You are logged in as root." || echo "You are not logged in as root."
Save and close the file. Run it as follows:
chmod +x test.sh
./test.sh
Sample outputs:
JAIL_ROOT is set to /www/httpd
You are not logged in as root.
Our previous example can be updated using source command as follows:
#!/bin/bash
# load myfunctions.sh function file
source /home/vivek/lsst2/myfunctions.sh

# local variable
quote="He WHO Sees me in all things, and ALL things in me, is never far from me, and I am never far from him."

# invoke is_root()
is_root && echo "You are a superuser." || echo "You are not a superuser."

# call to_lower() with ${quote}
to_lower ${quote}

A Note About Exit Status

This command returns the status of the last command executed in FILENAME; fails if FILENAME cannot be read. In this example, /etc/init.d/function exists and source using the following command:
source /etc/init.d/function &>/dev/null
echo $?
Sample outputs:
0
The exit status 0 indicate that source commanded successfully read /etc/init.d/function file. In this example, /etc/init.d/foo does not exists and source using the following command:
source /etc/init.d/foo &>/dev/null
echo $?
Sample outputs:
1
The exit status 1 indicate that source commanded failed to read /etc/init.d/foo file.

Comments

Popular posts from this blog

HAproxy logging

teamcity Automatic Agent Start under Linux

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all