TrumanWong

source

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

Summary

source filename [arguments]

The main purpose

  • Execute the file and load variables and functions from the file into the execution environment

Parameters

filename: file to be executed

arguments (optional): parameters passed to the file

return value

source returns the return value of the last command in the file. It will fail if the file cannot be read.

Incorrect usage

  • File not found in $PATH.

  • Document not given.

example

  • During the execution of some tools, the environment variable settings will be exported to into a file, and then use source to load the contents of the file into the execution environment.

  • Read and execute the /root/.bash_profile file.

[root@localhost ~]# source ~/.bash_profile

Q&A

Q: What is the difference between source and sh in executing files?

A: sh is executed in a subshell. source will cause the variables and functions of the executed file to be loaded into the current terminal environment (excluding variables modified by local in the function, etc.); it is recommended that you refer to the export command. Knowledge Points Section

Notice

  1. This command is a built-in bash command. For related help information, please see the help command.