Understanding BASH Shell (1)

Source: Internet
Author: User
Tags echo command

What is Shell?
This should be an interesting topic: "What is Shell ?』 I believe that most of my friends who have been familiar with computers and have heard of the concept of a job system, whether it is Linux, Unix or Windows. As long as they have a "job system", Shell is indispensable. However, before discussing Shell, let's take a look at the computer's operation status! For example, when you want a computer to transmit "Music", what does your computer need?
1. Of course, your hardware must be equipped with the "sound card chip" hardware; otherwise, how can the sound be generated;
2. The core of the operating system can support this chip group. Of course, the chip driver needs to be provided;
3. the user must be you.) input the voice instruction!
This is the basic step for outputting sound! That is to say, you must "enter" an instruction before "hardware" can work through your instruction! Hey! How does the hardware know your commands? That is the core of the kernel! Do you understand? That's right! That is to say, we must use Shell to communicate the commands we entered with the Kernel so that the Kernel can control the hardware to work correctly! Basically, we can illustrate the following two figures:
 
Basically, what we work for us is hardware, and what we control hardware is core, we use "Shell" to control the "tool Utility" provided by some kernel to manipulate hardware for us to do the right thing. For another example, when we use the shell function and input "cd/" to switch the directory, shell uses the cd tool provided by kernel to tell kernel to convert the hard drive's working directory to/, and then the hard drive is switched to/Based on the control of the kernel! This is the main function of Shell! Furthermore, since kernel does not understand human languages, and humans cannot directly remember the kernel Language, the communication between the two needs to be supported by shell!
Literally speaking, kernel is the meaning of "core", while Shell is the meaning of "Shell! That is to say, shell is the outermost louddb! The kernel is the innermost layer of the kernel! The core is the bottom layer of the job system! This core contains a variety of hardware support tools! Of course, if your hardware is too new and your kernel does not support it, I'm sorry, how powerful is your Shell capability, and there is no way to make the hardware work! Can this be understood? Haha! That's right! It is the core task to make the computer host work, but the core of the operation is to work for the user, but it is shell! Therefore, when your shell has been around for a long time, but the hardware cannot work, please note that your "core" is correct? Ah! Far away! This is what kernel is about ~~
BASH Shell
After knowing what Shell is, let's take a look at which shell is used in Linux? What! Which one? Cannot shell be a shell ?』 Haha! That's not it! Since the early Unix age, there were many developers! Therefore, shell has many versions based on different developers, such as the frequently heard Bourne SHell (sh), the C SHell preset in Sun, and the commonly used K SHell in business ,, there are also TCSH and so on. Each Shell has its own characteristics. For Linux, this version is called "Bourne Again SHell (bash)". This Shell is an enhanced version of the Bourne Shell and developed based on the GNU architecture!
Before introducing the advantages of shell, let's talk about the simple history of shell: the first popular shell was developed by Steven Bourne. to commemorate it, it is called the Bourne shell, or directly referred to as sh! Later, another widely used shell was designed by Bill Joy of the University of Berkeley to be attached to the shell in the BSD Unix system. The shell syntax is a bit similar to the C language, therefore, the name must be C shell, or csh for short! In the academic world, Sun hosts are quite powerful, while Sun is mainly one of the branches of BSD, so C shell is another very important and widely spread shell, because too many programmers use the C language !)!
Okay, so what is BASH? This shell is one of the most important tools and software in the GNU program. It is also the standard shell in the GNU operating system and is compatible with sh. As you can imagine, almost all Linux distribution currently uses bash as the main shell for management! So what are the advantages of this shell? Why should Linux use it as the preset shell? BASH has the following advantages:
· Command compilation capabilities are similar to DOS's doskey function): In bash, I think a great function is "he can remember the commands he used !』 This feature is really great! Because I only need to press "Up and down" in the Command column to find the previous command! The preset instruction memory function can reach 1000! That is to say, all the commands you have issued have been recorded, and the recorded files are in your home directory. bash_history! The commands executed during this logon are saved in the temporary storage area. After the system is successfully logged out, the commands are recorded in. bash_history! What functions does this have? The biggest benefit is that you can "query previous actions !』, So that you can know your execution steps, you can track your commands and use them as a debugging tool! However, there is also an annoyance that if a hacker intrude into the system, he only needs to go through the commands you have executed, just because your commands are related to the system, for example, directly entering the MySQL password on the command column), you can easily crack your Linux host! Therefore, it is better to reduce the number of recorded commands!
· Full-function file comparison and supplement for data correctness): this function is also quite good! If you do not want to press too many buttons during command execution, for example, the command pcprofiledump is long enough! Well, if you press the [Tab] button after entering pcprofile, bash will automatically connect the dump next to it! What if there are repeated commands? Press [Tab] twice to list all repeated commands!
O directly press [Tab] [Tab] twice in the Command column, then Linux will list all execution files in the system;
Another advantage is that you can compare the parameter carried out by this command with the path or file) to list the correct items! For example, if I want to list all files in/etc/sysconfig/network-scripts, how do I issue parameters?
O ls-al/etc/sysconfig/network-scripts
Then I can press the [Tab] button when entering the network, then-scripts will be automatically added to the command column! It's really convenient. It's a good thing to press the [tab] button if you have something to do!
· Command alias (alias) setting function: If I Need To Know all files under this directory contain hidden files) and all file attributes, so I have to issue command columns like ls-al, alas! Really troublesome. Is there a faster alternative? Haha! Just use the command alias! For example, I like to replace the above command with lm, that is, lm will be equal to a function like ls-al. Hey! What should we do? Just use alias! You can list the alias commands to know the alias of the current commands! You can also directly issue a command to set the alias:
Oalias lm = 'LS-al'
· Work control (jobs) and foreground background control: This part will be mentioned later in the instruction section! Before use, background control can make the work more smooth! Jobs are more widely used and can be used at any time! You are not afraid to accidentally use [Ctrl] + C to stop the program! It's really amazing!
· Powerful features of Shell scripts: Do you still remember to write a bunch of commands together in the DOS era? In Linux, shell scripts provides more powerful functions. It can write the continuous commands that you often need in your daily life into a file, this file can be used to detect hosts in interactive mode! You can also use the environment variables and related commands provided by shell to design them. Wow! The whole design is almost a small programming language! This scripts feature is beyond my imagination! What I used to write in a program language under DOS can be achieved through simple shell scripts in Linux! Really interesting !! Let's talk about this part now!
After learning about the advantages of BASH, we will discuss how to issue commands in the environment provided by Shell? In fact, it is very easy to issue commands in the following ways:
 
Easy! OK! Then let's ask another question: "When does Shell take over the Linux host !?』 If you have a little understanding of the "Boot Process" mentioned earlier by laruence, you should be able to understand that Linux has been executed through the Kernel, after executing some scripts set in init and run-level, the login program will be executed, and you will be able to smoothly access the program taken over by shell! Of course, you can use shell to communicate with the host only after you successfully log on to Linux! In addition, you must note that after logging on to the host, the user's initial directory is usually under the "home directory", and the root home directory is under the/root directory, the home directory is related to the setting of/etc/passwd!
Variables and variable settings: echo, env, set, variable setting rules, export, unset,
Before continuing to study BASH, we should discuss the variable, because there is too much information in the host to be accessed, and these materials are required by some services, for example, the mail access path is/var/spool/mail, and the home directory is preset at/home/useraccount. Of course, we can change these variables, however, if the variable is directly rooted in the suite, then when you modify some parameters, hey! Your suite must be "updated and compiled by the original code! This seems very troublesome, so there will be a variable. This is a good thing!
 
For example, the smtp storage path of sendmail is set through mail = "/var/spool/MAIL/$ USER" in/etc/profile, when I modified the above one, and then restarted, hey! My emails can be stored in different paths! And there will be no problems! You can send and receive data smoothly on a Linux host. However, the problem occurs on the pop3 Service. The default pop3 path is in the source code, and it is the path/var/spool/mail, that is, no matter how I modify my "variable", pop3 is not moved! Alas ~ It's really miserable, so it's impossible to directly receive emails with pop3. For example, OutLook cannot work )! The password is unacceptable!
In addition, for example, how does the system know where your ls command is stored when we execute a program? It turns out that there is a PATH variable, and the system will find the command system in sequence through the PATH set in this variable. If it cannot be found, "command not found" is displayed on the screen! These are just the purpose of the system's preset variables. For example, if you want to write a large script batch file, some materials may vary due to user habits, such as path !), Because this content is used in many places in the script, if you need to modify this place every time, it will be crazy! Use a variable at this time, and write the definition of the variable at the beginning! You only need to modify a row to modify the entire script! Convenient! Therefore, good programmers make good use of variable definitions! This part will be mentioned below !)
To learn more, in Linux System, all execution continuation requires an Execution Code. As mentioned above, you "actually use shell to communicate with Linux, after logging on to Linux correctly! 』 At this time, you will have a bash execution program, so that you can really communicate with the system through bash! Before entering shell, as mentioned above, the system needs some variables to provide access to other data or set the parameter values in some environments, for example, whether to display the color, etc.), so there are some so-called "environment variables" that need to be read into the system!
After talking for so long, what is "variable? To put it simply, "a variable is a set of characters or symbols to replace some settings or a string of retained materials !』, For example, "VBird" is "Bird brother", so when you read VBird, the system will naturally know it! Ha! That's laruence! The simplest example can be illustrated by PATH! If you are still impressed with the relative path and absolute path, you should know that you must specify the path and file name to issue correct commands! For example, if your ls command must be executed by "/bin/ls", why can you execute ls in any path? Instead of specifying a path? This is because the system has preset some "Search PATH", so when you need to execute some commands, the system will search for the commands according to the PATH settings! This PATH is a variable! So how to "display variables? This requires the echo command!
· Echo
Show variable content
Syntax:
 
Just like the example above, echo is used when we want to display the current PATH variable, and to determine whether it is a variable, in Linux, a "$" symbol is added before the preset variable name, so you must write it as echo $ PATH!
· How many environmental variables are there? Use env and set to see the following:
This is what we are interested in. How many environment variables are there in Linux? Haha! You can simply use env to understand it! "Basically, in the case of a Linux preset, the variable set by {uppercase letters} is generally the variable specified in the system." The following lists the preset variables of Red Hat 7.2:
· Env
Displays the main preset variable content in the current system.
Syntax:
 
 
The env command mainly reads the main variables in the current system! But I can sort the data by adding sort! So information like PATH, USER, HOME will be read out! In addition to the env command, there is also an instruction that can read all the variable data in the current system, called set! In addition to reading the above information, set also reads the additional information, which is usually related to user settings !) :
· Set
Display All variable content in the current system
Syntax:
 
In addition to displaying the preset values of the system, the use of set also shows all the variables you set! At the same time, it should be noted that if many people are online at the same time, your variables can only be used for yourself unless you change the system's preset parameter file, such as/etc/profile), without interfering with others! As mentioned above, because you will get a PID after logging on to Linux, and your settings will only be related to this PID and the subroutine! In addition, if the variable settings for this login are not changed, the variable set for this login will be canceled next time because the PID of the program is missing !)! So, if you want your variables to be automatically set every time you log on, you must write your settings to the configuration file loaded during login!
· Variable setting rules:
Well, we know some preset variables of the system, but if I want to set some of my own variables, how should I set them? What rules need to be followed? Haha! Before explaining this, you may want to know why you want to set variables?
My case 1: The simplest example is "path name! Taking laruence as an example, I used a Unix system to simulate some numerical models. However, due to the large amount of data, I was afraid that I would forget the contents and main meanings of this directory in the future, therefore, all my files have a long name, but the directories are often switched during the execution mode! I am crazy, just a few lines of path names will go crazy! So I set those lines of directory names to a four-character variable, so I just need to enter the "cd $ VARI" command, hey! Move to this path now! Very convenient! Of course, the variable is more than that, but this is the simplest example!


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.