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
sourcePurpose: 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
source filenameor
. filenamefilename: The path to the file containing the commands to be executed.
Common Scenarios for Using source
sourceLoading Environment Variables:
You can define environment variables in a file and use
sourceto load them into the current shell:# envvars.sh export PATH=$PATH:/usr/local/bin export EDITOR=nanosource envvars.sh
Setting Up Aliases and Functions:
Aliases and functions defined in a script can be loaded into the current shell session:
# aliases.sh alias ll='ls -l' alias la='ls -a'source aliases.sh
Sourcing Configuration Files:
Configuration files like
.bashrcor.profileare often sourced to apply settings to the shell:source ~/.bashrc
Running Initialization Scripts:
Initialization scripts that set up the environment for specific tasks can be sourced:
# setup.sh export PROJECT_HOME=/home/user/project cd $PROJECT_HOMEsource setup.sh
Important Notes
Current Shell Context: Unlike running a script directly (e.g.,
./script.sh), which executes in a new shell,sourceruns 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
errexitoption is set (set -e).Portability: While
sourceis widely used inbash, it is not POSIX standard. The dot command (.) is more portable and works in POSIX-compliant shells.
Examples
Simple Example:
# greetings.sh echo "Hello, $USER"source greetings.shOutput:
Hello, <your-username>Complex Example:
# setup_env.sh export DB_HOST=localhost export DB_USER=root export DB_PASS=secretsource setup_env.sh echo $DB_HOST # Output: localhost
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