source Command in Linux
Build an AI-First Career, Master the Complete Skillset
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
AI Forward Deployed Engineer Program
Full-stack engineering, production AI and client-facing consulting
+1000 moreOverview
The source command in Linux is a built-in shell command used to read and execute commands from a file in the current shell environment. The main purpose of this command is to apply the changes made in a configuration file without needing to logout or restart the system. One of the common uses of the source command in Linux is when modifying shell variables or adding paths to the PATH variable.
Syntax of source Command in Linux
The basic syntax of the source command in Linux is as follows:
Where:
- source: This is the command itself that tells the shell to read and execute commands from a file.
- filename: This is the name of the file from which commands are read and executed.
Options in source Command in Linux
How Scaler Transformed Careers in Different Fields
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Example Usages
-
Setting Environment Variables:
Output:
Explanation: In this example, we are creating a file named 'testfile' and adding an environment variable 'VAR' to it. We then use the source command to load the variables from 'testfile' into the current shell. Finally, we print the value of 'VAR' which outputs 'Hello World'.
-
Adding a Path to PATH Variable:
Output:
Explanation: This example demonstrates how to add a new path to the PATH variable. We create a file 'pathfile' and add a new path to the PATH variable. After sourcing 'pathfile', the new path is added to the PATH variable of the current shell.
Tips
-
Remember that the changes made using the source command are temporary and will only persist for the duration of the current shell session.
-
Always use the source command after modifying shell configuration files like .bashrc, .bash_profile or .env to apply the changes immediately.
Turn Learning into Career Growth
Advanced Use Cases of source Command in Linux
-
Running a Script in the Current Shell:
Output:
Explanation: This example demonstrates how to run a script in the current shell. We create a script 'script.sh' and give it execute permissions. When we source 'script.sh', it runs in the current shell and prints 'Hello from script'.
-
Sourcing a Script with Arguments:
Output:
Explanation: In this example, we create a script 'greet.sh' that takes one argument. When we source 'greet.sh' with the argument 'World', it prints 'Hello World'.
-
Sourcing a Script from a Different Directory:
Output:
Explanation: In this example, we demonstrate how to source a script from a different directory. We create a directory 'myscripts', create a script 'myscript.sh' in that directory, and give it execute permissions. When we source 'myscript.sh' from the 'myscripts' directory, it runs in the current shell and prints 'Hello from script in another directory'.
Conclusion
-
The source command in Linux is used to read and execute commands from a file in the current shell environment.
-
There are no specific flags or options associated with the source command as it is a shell built-in command.
-
Changes made using the source command are temporary and only persist for the duration of the current shell session.
-
The source command is commonly used when modifying shell variables or adding paths to the PATH variable.