Shell and export commands

Source: Internet
Author: User

References:
Http://baike.baidu.com/view/2959759.htm
Http://blog.sina.com.cn/s/blog_62945c360100ffvk.html
Http://www.51testing.com /? 44957/action_viewspace_itemid_90758.html

Shell and export commands

Export command
Function Description: sets or displays environment variables.

Syntax: Export [-FNP] [variable name] = [variable setting value]

Note: When executing a program in shell, shell provides a set of environment variables. Export allows you to add, modify, or delete environment variables for subsequent programs. The effect of export is only true for this login operation.

Parameters:
-F indicates the function name in [variable name.
-N: Delete the specified variable. The variable is not actually deleted, but is not output to the execution environment of subsequent commands.
-P: list all environment variables that the shell assigns to the program.

After a user logs on to Linux, the system starts a User Shell. In this shell, you can use shell commands or declare variables, or create and run shell scripts. When you run the shell script program, the system creates a sub-shell. At this point, there will be two shells in the system, one is the Shell started by the system at login, and the other is the shell created by the system for running the script program. When a script program runs successfully, its script shell is terminated and can be returned to the shell before the script is executed. In this sense, you can have many shells, each of which is derived from a shell (called the parent shell. The variables defined in the sub-shell are valid only in the sub-shell. If a variable is defined in a shell script program, when the script is running, the defined variable is only a local variable in the script program, and other shells cannot reference it, to change the value of a variable in other shells, you can use the Export command to output the Defined variables. The Export command enables the system to define a copy of this variable when creating a new shell. This process is called variable output.

Example (1 ):
Vim a. Sh
Content:
#! /Bin/bash/
Echo "printf A. Sh" $ AA
Vim B. Sh
Content:
#! /Bin/bash/
AA = "123"
Export AA
# Call A. Sh
./A. Sh
Run:./B. Sh
The result is: "printf A. Sh 123"

Note: About the source command

Source command usage:
Reference: http://eagletff.blog.163.com/blog/static/116350928201002204933414/

Source filename
Purpose: read and execute the command in filename in the current bash environment.
Note: This command is usually replaced by the "." command.
For example, source. bash_rc and. bash_rc are equivalent.
Note: The difference between the source command and shell scripts is,
Source executes the command in the current bash environment, and scripts starts a sub-shell to execute the command. In this way, if you write the commands for setting environment variables (or alias) into scripts, it will only affect the sub-shell and cannot change the current bash. Therefore, through the file (command column) when setting environment variables, use the source command.

If example (1) is changed
Vim a. Sh
Content:
#! /Bin/bash/
AA "ABC"
Vim B. Sh
Content:
#! /Bin/bash/
AA = "123"
# Call A. Sh
Source A. Sh
Echo "printf B. Sh" $ AA
Run:./B. Sh
The result is: "printf B. Sh ABC"

Source command usage:
Source filename
Purpose: read and execute the command in filename in the current bash environment.
Note: This command is usually replaced by the "." command.
For example, source. bash_rc and. bash_rc are equivalent.

The source command (from the C shell) is the built-in command of the bash shell. The dot command is a dot symbol (From Bourne
Is another name of source. Similarly, the variables configured in the current script will also be used as the script environment. The source (or point) command is usually used to re-execute the initialization of the last modification.
Documents, such as. bash_profile and. profile. For example
If the term variable is modified, you can use the source command to re-execute the command in. bash_profile without logging out and logging on again.
For example, if you run export $ KKK = 111 In a script, if you run the script with./A. Sh, after the script is executed, you run echo $ KKK
No value is found. If you use source for execution, Then ECHO
Kkk = 111. Because the./A. Sh command is called to run the shell in a sub-shell, the structure is not reflected in the parent shell after execution,
Different from source, it is executed in this shell, so we can see the result.

A wonderful use of the source command

When compiling the core, it is often necessary to repeatedly enter a long string of commands, such
Make mrproper
Make menuconfig
Make Dep
Make clean
Make bzimage
.......
These commands are long and cumbersome. And sometimes it is easy to lose mistakes, wasting your time and energy. If these commands are made into a file and executed in sequence automatically
Users, It is very convenient. You can use the source command to do this. Its function is to treat the content of a file as a shell for execution. First in/usr/src/Linux-
2.4.20 create a file 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 established, after each compilation of the core, you only need to input in the/usr/src/linux-2.4.20
Source make_command
That's all. This file can also be used as a script with only a slight modification. Here is to let everyone understand the usage of source. If you are not using lilo to guide the system, you can remove the last two sentences. Configure your own boot program to guide the new kernel.

Commands in shell programming are sometimes the same as those in C. & Represents and, | represents or. Connect the two commands with &, for example
Make mrproper & make menuconfig
, Indicates that the second command can be executed only after the first command is successfully executed. Commands that have requirements on the execution sequence can ensure that the following commands will not be executed blindly once an error occurs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.