source
In Unix-like operating systems, the source
command is used to read and execute commands from a file in the current shell environment. This command is often used in shell scripting and managing environment configurations. Here’s an overview of the source
command and its usage:
Overview of source
source
Purpose: The source
command executes commands from a specified file in the current shell. It is commonly used to load environment variables, functions, and configurations into the current shell session.
Shell Compatibility: The source
command is built into most Unix-like shells such as bash
, zsh
, and ksh
. In csh
and tcsh
, the equivalent command is .
(dot).
Usage
or
filename: The path to the file containing the commands to be executed.
Common Scenarios for Using source
source
Loading Environment Variables:
You can define environment variables in a file and use
source
to load them into the current shell:
Setting Up Aliases and Functions:
Aliases and functions defined in a script can be loaded into the current shell session:
Sourcing Configuration Files:
Configuration files like
.bashrc
or.profile
are often sourced to apply settings to the shell:
Running Initialization Scripts:
Initialization scripts that set up the environment for specific tasks can be sourced:
Important Notes
Current Shell Context: Unlike running a script directly (e.g.,
./script.sh
), which executes in a new shell,source
runs commands in the current shell context. This means any changes to environment variables, working directory, or shell options will affect the current shell session.Error Handling: If the sourced file contains errors, they will be reported in the current shell. The execution will continue after the error unless the
errexit
option is set (set -e
).Portability: While
source
is widely used inbash
, it is not POSIX standard. The dot command (.
) is more portable and works in POSIX-compliant shells.
Examples
Simple Example:
Output:
Hello, <your-username>
Complex Example:
Conclusion
The source
command is a powerful utility for managing shell environments, loading configurations, and initializing scripts. It plays a crucial role in shell scripting and session management, providing a flexible way to execute commands within the current shell context.
Last updated