Detailed explanation of source command under Linux

Original: https://blog.csdn.net/violet_echo_0908/article/details/52056071

source command usage

source FileName
  • 1

The role of the source command

Read and execute the commands in FileName in the current bash environment.

*Note: This command is usually replaced with the command ".".

Usage example:

source filename 

. filename(中间有空格)
  • 1
  • 2
  • 3


The source command (from the C shell) is a built-in command of the bash shell. The dot command, just a dot notation, (from the Bourne Shell) is another name for source.

Similarly, the variables configured in the current script will also be used as the environment of the script. The source (or dot) command is usually used to re-execute the just-modified initialization documents, 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 back in. For example, if you are in a script export $KKK=111 , if you use ./a.sh to execute the script, after the execution is completed, you run  echo $KKKit and find that there is no value. If you use source to execute it, and then echo, you will find that KKK=111. Because calling ./a.sh to execute the shell is run in a subshell, after execution, the structure is not reflected in the parent shell, but the source is different, it is executed in this shell , so you can see the result.

The source command (from the C shell) is a built-in command of the bash shell. A dot command, just a dot notation, (from the Bourne Shell) is another name for source. Both commands take as argument a script that will be executed as the environment of the current shell, i.e. will not start a new subprocess. All variables set in the script will become part of the current shell.


A wonderful use of source command

When compiling the core, it is often necessary to repeatedly enter a long series of commands, such as

make mrproper
make menuconfig
make dep
make clean
make bzImage
.......
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

These commands are long and cumbersome. And sometimes it's easy to make mistakes and waste your time and energy. If these commands are made into a file and they are automatically executed in sequence, it will be very convenient for users who need to repeatedly compile the core multiple times.

This can be done with the source command. Its role is to execute the contents of a file as a shell.

First create a file in the /usr/src/linux-2.4.20 directory, name it make_command, and enter the following content in it:

make mrproper &&
make menuconfig &&
make dep &&
make clean &&
make bzImage &&
make modules &&
make modules_install &&
cp arch/i386/boot/bzImge /boot/vmlinuz_new &&
cp System.map /boot &&
vi /etc/lilo.conf &&
lilo -v
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

After the file is established, every time you compile the core, you only need to input it under /usr/src/linux-2.4.20 source make_command . This file is also completely scriptable, with only minor changes.

Commands in shell programming are sometimes the same as in C. && means and, || means or. Connect two commands with &&, for example  make mrproper && make menuconfig, it means that the second command cannot be executed until the first command is executed successfully. Commands that require execution order can ensure that once an error occurs, the following commands will not continue to be executed blindly.


The difference between source filename and sh filename and ./filename execution script

  1. When the shell script has executable permissions, there is no difference between using sh filenameand ./filenameexecuting the script. ./filenameIt is because the current directory is not in the PATH, and all "." are used to indicate the current directory.
  2. sh filename Re-establish a subshell and execute the statements in the script in the subshell. The subshell inherits the environment variables of the parent shell, but the new and changed variables of the subshell will not be brought back to the parent shell unless export is used.
  3. source filename: This command simply reads the statements in the script and executes them in the current shell in turn, without creating a new subshell. Then all the new and changed variable statements in the script will be saved in the current shell.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326793023&siteId=291194637