source – Reads and executes commands from the specified file in the current shell environment

The source command is used to read and execute commands from a specified file within the current shell environment. The file can contain any valid shell commands, including variable assignments, function definitions, and other executable statements.

The syntax for using source is as follows:

source filename

or

. filename

Both commands are equivalent, with the latter being a shorthand notation for the former.

When executed, the contents of the specified file are read and executed as if they were typed directly into the current shell environment. This means that any changes to environment variables, aliases, or other shell settings made within the file will persist after the file has been executed.

Examples

Here are some examples of how to use the source command:

  1. To execute a script named myscript.sh in the current shell environment:
source myscript.sh
  1. To execute a script named myscript.sh using the shorthand notation:
. myscript.sh
  1. To execute a script located in a different directory:
source /path/to/myscript.sh
  1. To execute a script with a relative path:
source ./myscript.sh

Options

The source command does not have any options.

Troubleshooting Tips

  • If you receive an error message stating that the specified file cannot be found, verify that the file exists and that you have permission to read it.
  • If the file contains syntax errors or other issues, you may receive error messages when executing the source command. Review the contents of the file to identify and correct any issues.

Notes

  • The source command is often used in shell scripts to load configuration files or other scripts into the current shell environment.
  • The . notation for the source command is a shorthand notation that is commonly used in shell scripts for brevity.