Open In App

source Command in Linux with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

If you’re new to the world of Linux, you might have heard about commands that do various tasks, but some like the ‘source’ command might seem a bit confusing at first. Don’t worry; let’s break it down step by step.

What is the Source Command?

The source command in Linux is like a magic wand that lets you bring in commands from a file and use them directly in your terminal. It’s like having a recipe book and being able to use the recipes right away without copying them down.

The ‘source’ command is a built-in feature of the shell, designed to execute commands stored within a file directly in the current shell environment. When you use the ‘source’ command, it reads the contents of the specified file, typically a sequence of commands, and executes them as if they were typed directly into the terminal. This process happens within the context of the current shell session, without spawning a new process or interpreter. If any arguments are provided with the ‘source’ command, they are passed as positional parameters to the commands within the sourced file. However, the positional parameters of the current shell session remain unchanged unless explicitly modified by the commands within the sourced file. The ‘source’ command looks for the specified file in directories listed in the $PATH environment variable. If the file is not found in any of these directories, it will search in the current directory. It’s important to note that the ‘source’ command does not offer any options; its only argument is the name of the file to be sourced

Syntax of Source Command in Linux

The syntax of the source command is straightforward:

source FILENAME

Or its equivalent:

. filename

Here, ‘filename’ refers to the file containing the commands to be sourced.

Example of Source Command in Linux

Execution of Shell Scripts:

One of the primary use cases of the source command is to execute shell scripts within the current shell environment. This allows the script to manipulate variables, functions, and other shell features directly.

Imagine you have a shell script named ‘myscript.sh’ that sets a variable and then echoes its value:

# myscript.sh
#!/bin/bash
# Set a variable

MY_VARIABLE=”Hello, from myscript.sh”

# Echo the variable

echo $MY_VARIABLE

Now, let’s use the source command to execute this script within the current shell:

source myscript.sh

Executing a shell script

Executing a shell script

Explanation:

  • The source command executes the commands within ‘myscript.sh’ directly in the current shell environment.
  • The variable ‘MY_VARIABLE’ is set within ‘myscript.sh’ and echoed, producing the output within the current shell session.

Modifying Environment Variables:

The source command is crucial for modifying the environment variables of the current shell session. This is especially useful when a script needs to export variables that affect the parent shell environment.

# environment_setup.sh
#!/bin/bash
# Export a variable

export MY_ENV_VARIABLE=”This is my environment variable”

Now, let’s use the source command to modify the environment variables within the current shell:

Modifying Environment Variable

Modifying Environment Variable

source environment_setup.sh

To verify the changes, you can echo the environment variable:

echo $MY_ENV_VARIABLE

Explanation:

  • The source command executes the script ‘environment_setup.sh’, which exports the variable ‘MY_ENV_VARIABLE’.
  • After sourcing the script, the environment variable ‘MY_ENV_VARIABLE’ becomes available in the current shell session, as evidenced by echoing its value.

Loading Configuration Files:

Many applications in Linux rely on configuration files. Utilizing the source command, these configuration files can be loaded directly into the current shell, ensuring that changes take effect immediately.

Consider a scenario where you’ve made changes to your ‘.bashrc’ configuration file, such as adding a new alias:

# Add an alias to .bashrc

alias myalias=’ls -la’

Now, let’s use the source command to load the updated configuration file into the current shell:

source ~/.bashrc

Now, you can use the newly added alias ‘myalias’:

myalias
Loading configuration file

Loading configuration file

Explanation:

  • The source command loads the updated ‘.bashrc’ file into the current shell session, incorporating any changes made to aliases or other configurations.
  • After sourcing ‘.bashrc’, the newly added alias ‘myalias’ becomes available for immediate use, as demonstrated by executing it and listing the contents of the current directory.

Defining Shell Functions:

Shell functions defined within a script can be made available in the current shell session by sourcing the script containing those functions.

Suppose you have a script named ‘my_functions.sh’ that defines a shell function:

# my_functions.sh
#!/bin/bash
# Define a shell function

my_function() {
echo “This is my custom function”
}

Now, let’s use the source command to make this function available in the current shell:

source my_functions.sh

Now, you can call the function directly within the shell:

my_function
Defining Shell Functions

Defining Shell Functions

Explanation:

  • The source command executes the script ‘my_functions.sh’, which defines a shell function named ‘my_function’.
  • After sourcing the script, the function ‘my_function’ becomes available for immediate use within the current shell session, as demonstrated by calling it and echoing its message.

Conclusion

In conclusion, the ‘source’ command in Linux serves as a powerful tool for seamlessly integrating commands, settings, and functions from external files directly into the current shell environment. Acting as a conduit between the terminal session and files containing instructions, the ‘source’ command executes the commands within the specified file as if they were typed directly into the terminal. This enables dynamic modification of environment variables, execution of shell scripts, loading of configuration files, and availability of custom shell functions within the current shell session. By harnessing the capabilities of the ‘source’ command, Linux users can enhance productivity and efficiency in system administration and development tasks, ushering in a new level of command-line prowess and flexibility.



Last Updated : 19 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads