source Command in Linux

Learn via video courses
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Overview

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.
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Options in source Command in Linux

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.