Linux source Command


Introduction

The Linux source command is an essential tool for Linux users and administrators. It is used to execute a script file in current shell environment, and it allows you to modify current shell environment in same way that you would if you had typed commands manually. In this article, we will explore Linux source command and its various applications.

What is Linux source command?

The Linux source command is a shell command that reads and executes commands from a file in current shell environment. file is typically a shell script, but it can also be any text file containing a series of commands. source command is often used to set environment variables, define functions, and execute initialization scripts.

Syntax of Linux source command

The syntax of Linux source command is straightforward −

source filename

In this syntax, filename parameter represents name of file containing commands that you want to execute.

Using Linux source command

To use Linux source command, you must first create a script file containing commands that you want to execute. Once you have created script file, you can use source command to execute commands in file.

Here's an example −

#!/bin/bash
export MY_VAR="Hello World"

In this example, we've created a simple script file that defines an environment variable called MY_VAR and sets its value to "Hello World". To execute this script using source command, we would run following command −

source myscript.sh

This command would read contents of myscript.sh file and execute commands in current shell environment. After running this command, MY_VAR environment variable would be set to "Hello World".

The source command can also be used to execute initialization scripts. For example, many Linux systems include an initialization script that is executed when system boots up. You can use source command to execute this script manually if you need to −

source /etc/init.d/rc.local

This command would execute rc.local script located in /etc/init.d directory.

Using Linux source command with functions

The Linux source command is particularly useful when working with shell functions. Shell functions are a way to group a set of commands together and give them a name. You can then call function by its name to execute commands.

Here's an example of a simple shell function −

myfunction () {
   echo "Hello World"
}

In this example, we've defined a function called myfunction that simply echoes message "Hello World". To execute this function using source command, we would run following command −

source myscript.sh
myfunction

This command would read contents of myscript.sh file and define myfunction function in current shell environment. We could then call function using its name to execute commands.

Using Linux source command with variables

The Linux source command can also be used to set and modify variables in current shell environment. For example, let's say we have a script file called myvars.sh that defines two variables −

#!/bin/bash
MY_VAR1="Hello"
MY_VAR2="World"

To set these variables in current shell environment using source command, we would run following command −

source myvars.sh

After running this command, MY_VAR1 and MY_VAR2 variables would be defined in current shell environment, and we could access their values using $MY_VAR1 and $MY_VAR2 variables.

Using Linux source command to modify PATH variable

The Linux source command can be particularly useful when modifying PATH environment variable. PATH variable is used by shell to locate executable files, and it typically includes a list of directories separated by colons.

To modify PATH variable, you can create a script file that defines new PATH value and then use source command to execute script in current shell environment. Here's an example of a script file that adds a directory to PATH −

#!/bin/bash
export PATH="/my/new/directory:$PATH"

In this example, we've added /my/new/directory directory to beginning of PATH variable. To execute this script using source command, we would run following command −

source mypath.sh

After running this command, PATH variable would be updated to include new directory at beginning of list.

Additional Subheadings and Examples

Using Linux source command to source bash aliases

The source command can be used to source Bash aliases. An alias is a way to create a shorter name for a longer command, making it easier to type and remember. To use source command to source Bash aliases, you would create a Bash file with alias commands and then run source command on that file. Here's an example −

# in your ~/.bash_aliases file:
alias ll='ls -alF'

# source ~/.bash_aliases file
source ~/.bash_aliases

Now, when you run ll command in your terminal, it will be translated to ls -alF.

Using Linux source command in shell scripts

The source command can also be used in shell scripts to load environment variables and functions from other files. This can be useful when you have common functionality that you want to use across multiple scripts. Here's an example −

# in your common_functions.sh file:
my_function() {
   echo "Hello World!"
}

# in your script.sh file:
source common_functions.sh
my_function

In this example, source command loads my_function function from common_functions.sh, which is then called in script.sh.

Using Linux source command in virtual environments

The source command can be used in virtual environments to activate them. Virtual environments are isolated Python environments that can have their own installed packages and versions. source command can be used to activate virtual environment and load its dependencies. Here's an example −

# create and activate a virtual environment
python -m venv myenv
source myenv/bin/activate

In this example, source command activates virtual environment created in myenv directory.

Using Linux source command with Git

The source command can be used with Git to load aliases and functions that can make working with Git easier. Git aliases are shortcuts for longer Git commands. Here's an example of how to use source command with Git −

# in your ~/.gitconfig file:
[alias]
   st = status
   co = checkout
   br = branch

# source ~/.gitconfig file
source ~/.gitconfig

In this example, source command loads Git aliases defined in ~/.gitconfig.

Conclusion

The Linux source command is a powerful tool that can be used to execute script files in current shell environment. It is particularly useful when working with shell functions and modifying environment variables such as PATH. By using source command, you can modify shell environment in same way that you would if you had typed commands manually. This can save you time and make your work more efficient.

Updated on: 24-Mar-2023

21K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements