shell's export command

shell and export command
After the user logs in to the Linux system, the system will start a user shell. In this shell, you can use the shell command

Or declare variables, or create and run shell scripts. When running a shell script, the system creates a subshell.

At this point, there will be two shells in the system, one is the shell that the system starts when logging in, and the other is created by the system for running script programs.

shell. When a script finishes running, the script shell terminates, returning to the shell it was in before executing the script.

 

In this sense, a user can have many shells, each of which is forked from some shell (called the parent shell).

Variables defined in a subshell are only valid within that subshell. If a variable is defined in a shell script,

When the script program runs, the defined variable is only a local variable in the script program, and other shells cannot reference it.

To make the value of a variable can be changed in other shells, you can use the export command to output the defined variable.

 

The export command will cause the system to define a copy of this variable each time a new shell is created.

This process is called variable output.

[example]

In this example, the variable myfile is defined in the dispfile script.

Then use the export command to export the variable myfile to any subshell, such as the one spawned when the printfile script is executed.

List of dispfile scripts:

 

myfile=”List”
export myfile
echo “Displaying $myfile”
pr –t –n $myfile
printfile

 
Listing of the printfile script:

echo “Printing $myfile”
lpr $myfile&

 

operation result:

$dispfile
Displaying List
1 screen
2 modem
3 paper
Printing List
$

 

Export  function description: Set or display environment variables.
Syntax: export [-fnp][variable name]=[variable setting value]
Supplementary Note: When executing a program in the shell, the shell will provide a set of environment variables. Export can add, modify or delete environment variables for subsequent execution of programs. The effect of export is limited to this login operation.
Parameters:
 -f represents the function name in [variable name].
 -n Deletes the specified variable. The variable isn't actually deleted, it's just not output to the execution environment of subsequent instructions.
 -p List all environment variables assigned to the program by the shell.

 

Shell environment and variable lifetime understood from learning the export command

  Recently, I encountered a problem when I was learning the export command of BASH (the book says that export is to turn custom variables into

System environment variables): I define a variable in a script file, and then export the variable, according to my own ideas,

After executing the script, echo must be able to display its value at the prompt, but the result is not the case, after the script is executed

You can't see the existence of this variable with set at all. why? I was puzzled, and finally posted the problem, a senior told

I said that using source + script file is fine, I tried it and sure enough, but a new problem came out again. I put in the script

After the export command is deleted, the source can be used as well. That export doesn't seem to be of any use.

  Found something after many attempts, it's my own guess, if there is anything wrong, please correct me, thanks.

  When executing a script, a subshell environment will be opened first (I don't know if other programs are executed), and then the parent

All system environment variables in the shell are copied, and the statements in this script are executed in the subshell. (that is, the parent shell

The environment variable can be called in the subshell, but not vice versa. If the environment variable is defined in the subshell, it is only valid for the shell
or its subshell. When the subshell ends, it can also be understood as the script execution. When finished, the variable disappears. )

To demonstrate this, look at the script content:

  After the script such as test='value'
  export test
  is executed, test does not actually exist. Then look at the following:
  test='value'
  export test
  bash

  Here, open a subshell on the last line of the script. The shell should be the subshell of the shell where the script file is located. This script

After execution, you can see the variable test, because it is now in its subshell. When exit is used to exit the subshell,

The test variable disappears.

  If you use source to execute the script, if you do not add export, you will not see this variable in the subshell,

Because it is not a system environment variable, for example, the script content is: After
  test='value'
 
 is executed with source, this variable can be seen in the shell, but when bash is executed to open a subshell, test will not

It is copied to the subshell, because the execution script file is actually run in a subshell, so I build another script

When the file is executed, nothing will be entered, such as: echo $test. So pay special attention to this point, obviously at the prompt

You can use echo $test to output the variable value, why not put it in the script file?

  So the conclusion is:

1. When executing the script, it runs in a subshell environment, and the subshell automatically exits after the script is executed;

2. The system environment variables in a shell will be copied to the subshell (variables defined with export);

3. The system environment variables in a shell are only valid for the shell or its subshells. When the shell ends, the variables disappear
(and cannot be returned to the parent shell).

3. Variables defined without export are only valid for this shell, and are also invalid for subshells.

 

Sorted out the post: Why is a script not executed directly and executed with source? This is also a problem I encountered myself. The original manual is as follows:

Read and execute commands from filename in the current shell environment and
return the exit status of the last command executed from filename.

See why it's different, right? Direct execution of a script file is run in a subshell, and source is in

Running in the current shell environment. According to the previous content, you have already understood the reason.

 

Guess you like

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