The source command in Linux is used to execute commands from a file in the current shell environment. Its primary purposes include:
-
Executing Scripts: It runs a script file without creating a new subshell, allowing any variables or functions defined in the script to remain available in the current shell session.
-
Setting Environment Variables: You can use
sourceto load environment variables defined in a script, making them accessible in your current session. -
Updating the Current Shell: If you make changes to a configuration file (like
.bashrcor.zshrc), you can usesourceto apply those changes immediately without needing to restart the terminal.
Example Usage
To use the source command, you can run:
source script.sh
or simply:
. script.sh
Both commands will execute the script.sh file in the current shell.
