Usage of Linux command --source command

Original URL: Linux command -- source command usage_IT sharp knife unsheathed blog-CSDN blog

Introduction

        This article introduces the usage of the source command in Linux.

Introduction to the source command

The source command is a built-in command of the bash shell, which comes from the C Shell.
Another way to write the source command is the dot notation, which is the same as source and comes from the Bourne Shell.
The source command can force a script to immediately affect the current environment.
The source command enforces all commands in the script, regardless of file permissions.
The source command is usually used to re-execute newly modified initialization files, such as .bash_profile and .profile and so on.
The source command can affect the environment of the parent shell executing the script, while export can only affect the environment of its subshells.

        The source (or dot) command is usually used to re-execute newly modified initialization files, such as .bash_profile and .profile and so on. For example, if you make changes to the EDITER and TERM variables in .bash_profile after logging in, you can use the source command to re-execute the commands in .bash_profile without logging out and logging in again.

        The source (or point) command is an internal command of the shell, which reads all command statements from the specified shell file and executes them in the current process. Therefore, when multiple shell processes (parent-child processes or unrelated processes) share a set of variable values, these variable assignment statements can be defined in a shell file, and the dot statement can be used in programs that need these variable values ​​​​to reference This shell file, so as to realize the sharing of variable values ​​(the modification of these variable values ​​​​only involves this shell file). But it should be noted that this shell file cannot include statements containing positional parameters, that is, command line parameters such as $1 and $2 cannot be accepted.

        As can be seen from the above, the source command is actually equivalent to #include in the c language.

example

Example 1: .bashrc takes effect immediately

source ~/.bashrc

or

. ~/.bashrc

The content in ~/.bashrc takes effect immediately after execution.

Example 2: Define variables and use

Screenplay (a.sh)

#! /bin/bash
export $KKK=111

Excuting an order

./a.sh;echo $KKK

Results of the

no information printed

Excuting an order

. ./a.sh;
echo $KKK

or

source ./a.sh;
echo $KKK

Results of the

KKK=111

Cause Analysis

./a.sh:

        If the shell where the current terminal is located forks a subshell and then executes a.sh, it will create its own process environment. When the process ends, the environment it created will also be destroyed. So after execution, return to the shell where the terminal is located, so after execution, the result is not reflected in the parent shell.

source a.sh:

        Executed in the current shell, so you can see the result.

Guess you like

Origin blog.csdn.net/feiying0canglang/article/details/127967869