Talking about Linux Shell

  
That day, my colleague showed me an operation step on my computer
, using an env command, well, I admit that I am illiterate, I don't know what this command is doing! ! ! It is because of my ignorance that it has aroused the desire to learn and explore. I must learn this env thoroughly. After several hours of free time after work, I finally figured out this “坨”; 东西 坨 rd 记录 记录 记录 记录 记录 记录 记录 记录 记录 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西 东西
The simplest understanding of Shell
This is the shell in your eyes. When you open Terminal in Linux, there is a black screen, or a white screen window. Oh, this is our eyes. Shell, this understanding may be right, maybe not right.

When we open Terminal, we actually run a default shell interpreter. We are usually /bin/bash. When we input various commands in the terminal, it is from /bin/Bash to explain; that is, Shell is a program running on Terminal.
Understanding the Shell, we often run a shell script in the current shell. When you run the shell script, do you know what's behind this? This goes on to the next topic: the parent shell and the child shell.
Parent Shell and Child Shell
If you don't understand the relationship between the parent shell and the child shell, then the variables in the shell that this article focuses on are not clear.
When we start Terminal, we will run a shell process, which is called Shell process A for the time being; when we run the shell script in this Terminal, process A will fork a new process to start another shell interpreter. (This is specified by the first line in the script, for example: #!/bin/bash), the new process from fork is called process B for the time being. At this point, process A and process B are the relationship between the parent and child processes; process B is a child shell, and process A is the parent shell. Once the script in the child shell has finished executing, the child shell ends and returns to the parent process. This is the parent shell and the child shell.
I think I should clear the parent shell and the child shell. After understanding this relationship, we will continue to summarize.
Shell initialization read some configuration files
First of all, you have to realize that you log in to the Linux system, open the Terminal, when using the Shell, the system reads a large "&" configuration Files that determine the variables in your shell. So, when we summarize the variables in the shell, we need to look at which configuration files are read and what the order of reading them is.
  • /etc/profile: This file sets the environment information for each user of the system. When the user logs in to the system for the first time, the file will be executed. And read the shell settings from the configuration file in the /etc/profile.d directory;
  • ~/.bash_profile or ~/.bash_login or ~/.profile: the system will look for these three files in turn, these It is for the configuration of the current user, but it should be noted that these three files generally do not exist at the same time. Even if they exist at the same time, after the system finds one in this order, it will not read the rest;
  • ~/.bashrc: This file contains the bash information for the bash shell that belongs to the currently logged in user. This file will be read when logging in and opening a new shell each time;
  • /etc /bashrc: Execute this file for each user running the bash shell; when the bash shell is opened, the file is read.

    These script configuration files determine your system variables and environment variables. If you are interested, you can check out the source code of these scripts and you will know exactly how they are called. Of course, if we need to define some variables we use often, such as configuring the JDK, you may want to edit them.
    Variables in Shell
    Play with the shell before, clear the variables in the single shell process, and then add the child shell to summarize.
    There are the following three variables in the Shell:
  • Internal variables; system definitions, can't be modified;
  • environment variables; system definitions, can be modified, you can use export to convert user variables to Environment variables;
  • user variables; user-defined, can be defined, nothing to play with.

    For example, the following are internal variables:

    Variable name
    Description
    $# Command line parameter number $0 Current program name $? Previous The return code of the command or function is the PID of the current program.
    The following are some of the environment variables we use. Generally, we use the env command to view the current user's environment variables.

    Variable Name
    Description
    PATH Indicates which directories the Shell will look for in the directory or program SHELL Current User Shell Type HOME Current User Home Directory PS1 Basic Prompt
    > User variables (local variables) is more casual, you can define it yourself, for example:
    > str='Hello World'> echo $str We use the set command to display the user defined in the current shell Variables, of course, the set command also outputs environment variables.
    Do you understand what you said above? We continue.


































    With parent and child relationships, will the shell variables in the parent process be inherited into the child process? ? ? At the same time, will the shell variable in the child process be returned to the parent process? ? ? These are all we need to pay attention to. Let's look at an example:
    The parent shell defines a variable:
    > str='Hello World' then runs the following script in the parent shell:
    #!/bin/bash# Output definition in the parent shell Strecho $str# Output environment variable echo $HOMEecho $PATH You will find that after running, $str output is empty, but $HOME and $PATH can be perfectly output. This also shows that the user variables we define in a shell can only be used by the current shell, and others are inaccessible, even if it is a child shell; the environment variables of the parent process can be used in the child process. access. But sometimes, we have this requirement:

    Accessing the parent process's user variables (local variables) in the child process, what should I do? ? ?
    When we encounter such a demand, I have to say something about the export command.
    Talk about the export command The
    export command sets the user variable to an environment variable so that it can be accessed in the child shell process. This is exactly the same as the Chinese meaning of export. For the previous example, we can enter the command in the parent shell:
    > export str When you execute the script again, you can output the value of the user variable str; this is the role of export. However, after we enter the export str in the parent shell, when we close the parent shell, the environment variable will be invalid. If you want to open the shell, you can set the export immediately. We can write the export str to the above summary according to our needs. That & ldquo; 坨 & rdquo; start the configuration file, so that will not cause the export to fail due to closing the parent shell.
    Another relationship between parent and child
    Now we can access the variable of the parent process in the child process. Have you ever thought about whether modifying the variable of the parent shell in the child shell will affect the variable in the parent shell? What is the value? May wish to do a test.
    #!/bin/bash# Modify the value of the variable str passed by the parent shell $str='http://www.jellythink.com'echo $str Run the script and find the value of str in the parent shell and No change has taken place. In fact, this is related to another knowledge point about the process in Linux. When the parent process forks a child process, the child process will copy the relevant variables of the parent process. At this time, the child process will have the same value as the parent process. Although the same name has the same value, it is only a copy of the parent process. The modification to the copy is independent of the parent process. For information about Linux processes, you can refer to this article.
    And if we define a variable in the child shell, is it accessible in the parent shell? Practice tells us that this doesn't work, you can't access the variables defined in the child shell in the parent shell. If you want to access the variables defined in the child shell in the parent shell, you can use a temporary file to write the local variables to the temporary file, and the parent shell reads the file to achieve the purpose of accessing the variables defined in the child shell.
    Summary
    Although this article is mixed and mixed, each part is closely related and interlocking. It is very helpful for everyone to understand Linux Shell as a whole, and it is also beneficial for everyone to learn Linux Shell. The article is a bit long and requires a little patience to read it from start to finish. In order to learn, it takes a little patience. What do you say? This one is summarized here, and the next one is goodbye.
    Jelly Think - an original technical article sharing site.
    October 21, 2015 in Hohhot.
    Appendix
    Here is a summary of the commands involved in the text and some related commands.

    Command
    Description
    set Shows the locally defined shell variable unset Clear environment variables, for example: unset str export Set a new environment variable env Display all current environment variables of the user

  • Copyright © Windows knowledge All Rights Reserved