What is the purpose of `source` command?

089

The source command in Linux is used to execute commands from a file in the current shell environment. Its primary purposes include:

  1. 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.

  2. Setting Environment Variables: You can use source to load environment variables defined in a script, making them accessible in your current session.

  3. Updating the Current Shell: If you make changes to a configuration file (like .bashrc or .zshrc), you can use source to 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.

0 Comments

no data
Be the first to share your comment!