The source command is a built-in command in Unix-like operating systems that is used to execute commands from a file in the current shell session. When you use source, the commands in the specified file are run as if they were typed directly into the terminal. This is particularly useful for loading environment variables, functions, and aliases defined in shell configuration files.
Key Features of the source Command:
-
Current Shell Context: Unlike running a script directly (e.g.,
./script.sh), which executes in a subshell,sourceexecutes the commands in the current shell. This means any changes made to environment variables or shell settings persist after the command completes. -
Common Use Cases:
- Loading configuration files (e.g.,
.bashrc,.zshrc) to apply changes without restarting the terminal. - Executing scripts that set up the environment for development or other tasks.
- Loading configuration files (e.g.,
Syntax:
source filename
or using the shorthand:
. filename
Example:
If you have a script named setup.sh that sets environment variables, you can run:
source setup.sh
After executing this command, any variables or functions defined in setup.sh will be available in your current shell session.
