Linux command introduction: source usage

  

This article describes the use of source commands in Linux

source FileName

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

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

For example: source .bash_rc is equivalent to . .bash_rc.

The source command (from the C shell) is a built-in command of the bash shell. The dot command is a dot symbol (from the Bourne Shell) is another name for source. Similarly, the variables configured in the current script will also serve as the environment for the script. The source (or dot) command is usually used to re-execute the newly modified initialization documents, such as .bash_profile and .profile. For example, if you modified 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 back in.

For example, if you export $KKK=111 in a script, if you execute the script with ./a.sh, after executing, you run echo $KKK and find no value, if you use source to execute Then echo, you will find KKK=111. Because the call to ./a.sh to execute the shell is run in a subshell, after execution, the structure does not reflect to the parent shell, but the source is different, he is executed in this shell, so you can see the result < Br>

The source command (from the C shell) is a built-in command for the bash shell. The dot command is a dot symbol (from the Bourne Shell) is another name for source. Both of these commands take a script as an argument, and the script will be executed as the current shell environment, ie a new child process will not be started. All variables set in the script will be part of the current shell. Similarly, the variables set in the current script will also be used as the environment for the script. The source (or dot) command is usually used to re-execute the initialization files just modified, such as .bash_profile and .profile. For example, if you modified 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 back in. Like .bash_profile or other similar shell scripts, files can be executed with source or dot commands without executable permissions.

A wonderful use of the source command

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

make mrproper

make menuconfig

make dep

make clean

make bzImage

.......

These commands are long and cumbersome. And sometimes it's easy to make mistakes and waste your time and energy. If you make these commands into a file and let it execute automatically in order, it will be convenient for users who need to repeatedly compile the core multiple times. This can be done with the source command. Its role is to treat the contents of a file as a shell. First create a file in /usr/src/linux- 2.4.20 directory, named make_command:

Enter the following content:

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

After the file is created, each time the kernel is compiled, Just type

source make_command

under /usr/src/linux-2.4.20. This file can also be completely scripted, with a few changes. The main reason here is to let everyone understand the usage of source. If you are not using lilo to boot the system, you can remove the last two sentences. Configure your own bootloader to boot the new kernel.

The commands in shell programming are sometimes the same as C. && means with, | | Indicates or. Join the two commands with &&, such as make mrproper && make menuconfig , to indicate that the first command is executed successfully to execute the second command. Commands that require execution order ensure that the following commands will not continue blindly in the event of an error.

Original address http://hi.baidu.com/linuxcer/blog/item/8462d1990827fc0a6e068c14.html

----------------- ------------------------------

-------------- -- My test----------------------

----------------- ------------------------------

1 Create test.sh

#!/Bin/bash

export s=/home/jboss/

2 Execute the command: source test.sh

echo $s

Result output: /Home/jboss/

3 Open a shell

Execute the command: ./test.sh

echo $s

Result: No output s value

Copyright © Windows knowledge All Rights Reserved