Open In App

source Command in Linux with Examples

Last Updated : 19 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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.

source Command in Linux with Examples – FAQs

What does the source command do in Linux?

The source command in Linux is used to read and execute commands from a specified file in the current shell environment. Essentially, it allows you to run the contents of a script as if they were typed directly into the terminal. This command is particularly useful for initializing variables and running configuration scripts that affect the current shell.

How to use the source command with a script?

To use the source command, simply type source followed by the path to the script file. For example, if you have a script named setup.sh in your current directory, you would use:

source setup.sh

This command will execute all the commands in the setup.sh script in your current shell environment. If the script sets environment variables, modifies the path, or defines functions, those changes will take effect immediately in the shell from which you ran the command.

What is the difference between source and ./ in Linux?

The key difference between source (or . which is synonymous with source) and executing a script with ./ lies in the environment in which the commands are executed:

  • source / .: When you source a script using source script.sh or . script.sh, it runs within the existing shell. This means that any environment variables or changes made within the script persist in the shell after the script completes. This method is ideal for initializing settings and variables in the shell.
  • ./: When you run a script using ./script.sh (assuming it is executable), it runs in a new subshell. Changes made to the environment in the script, such as setting variables or changing directories, do not persist when the script finishes. They only affect the subshell created for the script.

Can the source command be used to reload environment variables?

Yes, the source command is commonly used to reload environment variables. For example, if you modify your shell’s configuration files (e.g., .bashrc, .profile), you can apply the changes without needing to log out and back in or open a new terminal window by sourcing the file:

source ~/.bashrc

This command re-executes the contents of .bashrc, updating the current shell environment with any new changes.

What are some common use cases for the source command?

The source command is incredibly versatile and used in a variety of scenarios, such as:

  • Loading Environment Variables: It is often used in scripts to set or update environment variables that need to be available in the current shell.
  • Running Configuration Scripts: Users can source configuration scripts for software applications that need to set specific environment variables or define shell functions.
  • Development Workflows: Developers use source to load virtual environment configurations, especially in Python development, where virtual environments provide isolated Python instances for projects.
  • Automating Tasks: In automated scripts that prepare a shell environment, source can be used to ensure all settings are correctly initialized.


Next Article

Similar Reads

three90RightbarBannerImg