Understanding and learning Shell

Source: Internet
Author: User
Understanding and learning Shell

The shell version used in Linux is called "Bourne again shell (BASH)". This shell is an enhanced version of the Bourne shell and developed based on the GNU architecture.

BASH Shell functions

1. Command memory function: it can remember the commands used. The commands are recorded in. bash_history in the main folder. Note :~ /. Bash_history records the commands executed before the previous logon, And the commands executed during this logon are saved in the temporary memory. After logging out of the system, these commands are logged back to. bash_history.

2. Full functions of commands and files (benefits of the [Tab] key)

3. Command alias setting function (alias)

Set: alias LM = 'LS-al'

4. Job control, foreground, and background control

5. script program

6. wildcard :*

BASH Shell built-in command: Type

We already have many built-in commands in bash. Such as CD and umask.

So, how do we know whether this command comes from an external command or an internal command? In this case, we can use the type command to view the details.

Type can also be used for usage similar to the which command (which: Find executable files)

 

Shell Variable Function

Display and setting of variables: ECHO and unset

Variable display: Echo eg: Echo $ variable

Echo $ path

Variable settings: Just use "=" to connect the variable to its content.

Eg: myname = droidxin

For variable settings, we must master some variable setting rules.

 

The difference between single quotation marks and double quotation marks: Double quotation marks can still retain the content of variables, while special characters still keep their original features. However, single quotation marks can only be general characters without special characters. That is to say, a single quotation mark is used. It is not the content of a variable, but just a common character.

Anti-single quotes: in a string of commands, commands in the anti-single quotes will be executed first, and the execution results will be used as external input information.

Eg: ls-l 'locate wode'

 

Functions of Environment Variables

First, how many default environment variables do we know? You can use the env and export commands.

Next, let's take a look at some common environment variables.

 

Ps1, command prompt, a very interesting variable

$ (The PID of the Current Shell), which represents the thread code of the Current Shell, that is, the so-called PID (process ID)

? (About the return code of the previous command) generally, if the command is successfully executed, a value of 0 is returned.

Variable operation commands

Variable keyboard reading, array and Declaration: read, array, declare

Read: Read variables from keyboard input

Declare/typeset: Declares the type of the variable. The declaration of the variable is very useful. The default setting of the variable is string type. Sometimes you need to calculate the variable, you need to set the type of the variable to an integer.

Array variable type.

Ulimit: it is a restricted relationship with file systems and programs. It can restrict user resources, including the number of opened files, the CPU time available, and the total amount of memory used.

Delete, replace, and replace variable content. For more information, see reference p313.

Command alias and History: alias, unalias, history

BASH Shell operating environment

1. Now we know that there are actually a lot of LS commands in the system, or include built-in echo commands. Think about it. If a command is executed, which ls is used for execution? Basically, the order of command execution is as follows:

 

2. Bash login and welcome information:/etc/issue,/etc/motd

You can customize some user login display information, such as displaying some prompt words, which is very useful for multi-user management.

3. Bash environment configuration file

Bash directly reads the bash environment configuration file when it is started. The bash operating environment has been planned. Note that the command alias we set will expire after you log out of Bash. Therefore, if you want to save your settings, you need to write these settings to the configuration file.

Let's talk about Login Shell and non-login shell.

Login Shell: complete login process is required when Bash is obtained.

Non-login shell: you do not need to re-log on to the bash interface. For example, you do not need to log on again to the terminal from the graphical interface of X.

Login Shell only reads/etc/profile and ~ /. Bash_profile or ~ /. Bash_login or ~ /. Profile

/Etc/profile: overall system settings. It is best not to change

~ /. Bash_profile or ~ /. Bash_login or ~ /. Profile: the user's personal settings. Here is the data to be modified.

In addition, I will not mention the bash environment configuration file and how to configure it.

Wildcard and special characters

Wildcards make it easier and faster to use Bash

 

Special symbols in the bash Environment

 

 

 

 

 

Data Stream redirection

What is data stream redirection? Generally, if you want to execute a command, this process is usually required.

 

After a command is executed, the returned information will be displayed. If the command is correctly executed, the standard output will be used to output the correct Command Execution information, otherwise, standard error output is output, and the command execution error is output.

Data Stream redirection means to transmit the information to other files or devices separately.

 

Data Stream redirection is very important, and command operations are also diversified. You can refer to the reference of p329 in this document.

When to use data stream redirection

Now that we know Data Stream redirection, when should we use data stream redirection? Data Stream redirection is generally used in:

1. The screen output information is very important, and we need to save it;

2. The Program executed in the background does not want it to interfere with the normal output results of the screen;

3. The execution results of some system routine commands should be saved;

4. When some errors may be known during command execution, you want to block them with "2>/dev/null;

5. The error information and correct information must be output separately;

Command Execution judgment basis

Judgment basis:; & |

; Continuous command execution without considering command relevance

 

Pipeline command

The pipeline command "|" can only process the correct information sent through the previous command, that is, the standard output information. It has no direct processing capability for standard errors.

Select command: Cut, grep

Cut: part of the information

Grep: analyzes a row of information. If necessary, extract it.

Sorting command: sort, WC, uniq

Tee: bidirectional redirection

Character conversion command: TR, Col, join, paste, expand

Split: The cutting command to cut large files.

Xargs: parameter replacement

 

Important Review

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.