Linux shell structure with source command

Source: Internet
Author: User

Linux shell& Environment variables

when you log on to the system, a user shell is started. In this shell, you can use a shell command or declare a variable, or you can create and run a shell script program. When you run a shell script, the system creates a child shell. At this point, there will be two shells in the system, one for the system-initiated shell at logon, and the other for the shell created by the system to run the script. When a script is finished running, its script shell terminates and can be returned to the shell before the script executes. In this sense, a user can have many shells, each of which is derived from a shell, called the parent shell.
A variable defined in a child shell is valid only within that child shell. If a variable is defined in a shell script, when the script is run, the defined variable is only a local variable within the script, and its child shell cannot reference it, so that the value of a variable can be changed in its child shell, You can use the Export command to output a defined variable. The export command will allow the system to define a copy of this variable when creating each new shell. This process is called variable output. Example
Write a C program, the C program needs to dynamically link a user-written dynamic library. After everything has been compiled, run the executable file, and you will find that the dynamic link library cannot be found by the system prompt. OK, you find that the path to the dynamic library has not been added to Ld_library_path. However, after the addition, the system still indicates that the dynamic library cannot be found. What is the reason for this? When you use the Export Ld_library_path command, run the executable again, and it succeeds! It is speculated that the Linux shell also produces a child shell when it runs the executable program.   Source Commandwhen the source command executes a script, it does not produce a child shell, but runs in the current shell environment. For example, you export $KKK in a script = 111, if you use./a.sh execute the script, after execution, you run the Echo $KKK, found that there is no value, if you use source to execute, and then echo, you will find kkk=111. Because calling./a.sh to execute the shell is run in a child shell, the structure does not react to the parent shell after execution, but the source is different and is executed in this shell, so you can see the result.

Linux shell structure with source command

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.