Bash Source Command

Bash Source Command

The Bash source command reads and executes commands from a specific file as an argument within the current shell environment. It can be used to load variables, functions, and configuration files. The source command in bash has no options and only the filename is passed as an argument.

In this article, we will discuss the bash source command with examples.

Bash Source Command Syntax

The basic syntax of the bash source command is:

source FILENAME [ARGUMENTS]
. FILENAME [ARGUMENTS]

The command starts searching for the by following the path given with the filename. If the filename does not specify a path, then the command will search the directories that are specified in the $PATH environment variable. Finally, the command will look for the file in the current directory if there are no files found in $PATH.

Bash Source Command Exit Status

The exit code for the source command is:

  • 0 if the file exists.
  • 1 if the file does not exist.

Bash Source Command Example

Read Variables from a File

With the source command, you can read variables from a specific file. To do that, the variables must be defined and set using the syntax: VARIABLE=VALUE.

Create a config.sh File

var1="Bash"
var2="Source Command"

Read the config.sh File

If var1 and var2 are defined in config.sh, then you can read the configuration file and source the variables into your bash script:

source config.sh
echo "The value of var1 is $var1"
Output
The value of var1 is Bash

Sourcing Functions

If you use a particular function frequently in your shell scripts, then you can extract them into a file and source them into your scripts for convenience. Using the source command to source function makes your scripts smaller and cleaner without having to type the entire function every time you need to use it. Additionally, any changes you make in the functions file will be automatically applied to all scripts.

For example, we’ll place a function, in a script, that prints the time in 12 hour format, then we’ll source it so we can use the function in the command-line. We can have many other functions in this script, but we’ll just place the one for example purposes:

functions.sh
whatTimeIsIt () {
  output=$(date +'%H and %M minutes')
  echo "The time is ${output}"
}

To source it we run:

source functions.sh

Next we can just call the function in the terminal:

whatTimeIsIt
Output
The time is 14 and 23 minutes

Conclusion

We have discussed the shell built-in command – source, and its common uses Let us know in the comments section if you have any feedback or questions and we’ll get back to you as soon as possible.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
How to Use the Kill Command in Linux
Read More

The Kill Command in Linux

Each Linux system runs several processes. Not every process behaves in the manner we want them to. Let’s…