Shell scripts in Linux set environment variables __linux

Source: Internet
Author: User
shell scripts in Linux set environment variables - -How to set environment variables with scripts
I want to make a script to set up or cancel a set of environment variables, but because the script program's settings for variables disappear after exiting the script, there is no indirect way to implement this function.
You're talking about export.
Use export in a script as if it were only valid in a script, quit the script, and set the variable without
Set in your script to run in ~/.profile or ~/.BASHRC, etc with source
I tried, with source to run the normal shell script, but also to set the environment variable, to ask what source is the command. I didn't find the description with man and info.
Source is Bash's inline command, man bash/source filename \[arguments\]
Thank u

SOURCE filename or code:. FileName
Source is Bash's inline command, man bash/source filename \[arguments\]
Source doesn't seem to be bash's ' inline command ' forgetting which release. Source is an external command
$ type source source is a shell builtin
Which BT release has externalized source. Daze.. --------------------------------------------------I am also a rookie, touch Linux not long, I ran into one recently when I was learning about Bash's Export command. (the book says export is a custom variable into a system environment variable): I define a variable in a script file, then export variables, and, following my own thoughts, execute this script, At the prompt, it is possible to display its value with ECHO, but this is not the case, and the script does not see the existence of the variable with the set at all after execution. Why, then? I baffled, and finally posted the problem, a senior told me to use the source+ script file on it, I tried to do it, but a new problem came out again. After I delete the export command in the script, I can use source. Then this export doesn't seem to be of any use. After several attempts to find something, I guess, if there is anything wrong, please correct me, thank you. When executing a script, a child shell environment is first opened (not knowing if the other program is executing), and then all the system environment variables in the parent shell are copied, and the statements in the script are executed in the child shell. (That is, the parent shell's environment variable can be invoked in a child shell, but not the other way around, if the environment variable is defined in the child shell, only the shell or its child shell is valid, and when the child shell ends, the variable disappears when the script finishes executing.) To prove this, look at the script: after a script such as test= ' value ' export test executes, test does not actually exist. Then look at the following: test= ' value ' export Test bash here to open a child shell in the last line of the script, the shell should be the shell of the script file, which, when executed, can be seen with the test variable. Because it is now in its child shell, the test variable disappears when you exit the child shell with exit. If the script is executed with source, If you do not add export, you will not see this variable in the child shell, because it is not a system environment variable, such as the script content is: test= ' value ' is executed with source, you can see the variable under the shell, but when you execute bash to open a child shell, test is not copied to the child shell. Because the execution script file is actually running in a child shell, I will not enter anything when I build another script file, such as Echo $test. So this particular attention, clearly in the prompt can use echo $test output variable value, why put it into the script file is not. So the conclusion is: 1, the execution of the script is in a child shell environment, the script after the execution of the child shell automatically exit; 2, the system environment variables in a shell are copied into the sub shell (with the variables defined by export); 3. The system environment variable in a shell is valid only for the shell or its child shell, and the variable disappears at the end of the shell (and cannot be returned to the parent shell). 4. A variable that is not defined by export is valid only for the shell, and the child shell is also invalid. Later, according to the moderator's tips, sorted out the post: Why a script to execute directly and with the source execution is not a row. This is also a problem that I have encountered myself. Manual The original text is this: Read and execute commands from filename in the current shell environment and return the exit status of the LA St command executed from filename. See why it's not the same. Executing a script file directly is running in a child shell, and source is running in the current shell environment. According to the previous content, you have already understood the reason of it. The problems that have plagued me for a few days can finally be solved satisfactorily.

Turn from: http://www.newbt.net:8022/read.csp?tid=1306&fpage=1


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.