Linux Learning Summary (17)-shell Basics

Source: Internet
Author: User

The content of this article is very miscellaneous, but it is not difficult to understand, do not map by example.

First introduce several characters commonly used characters:

1 * matches any of any characters
2? Match an arbitrary character
3 # comment Symbol, the statement after the symbol is not executed
4 \ de-ideographic character, followed by the meaning of the character, according to the original character output
5 [] matches any one of the characters contained in []:
For example [Bc]ook matches book or cook
[0-9] Match any number
6 {.} matches any one character, same as [] effect, format different
7 > Output Redirection
such as Cat 1.txt > 2.txt, read 1.txt content to replace 2.txt content
8 >> pursuit of heavier orientation
Cat 1.txt >> 2.txt read 1.txt content and append to 2.txt tail
2> and 2>> error message output, if a command execution is unsuccessful, the output error message to the specified file. For example suppose there is no a.txt this file cat a.txt 2> Error.log
Note: This 2 calculates a truth value, when the command execution fails, the judgment is false, with 2 means
So the execution succeeds, the result is true, is 1, usually we omit this 1
&> and &>> Correct errors are output, of course, output to the same file, if you want to separate, you can
Cat 1.txt a.txt > 2.txt 2> error.log
< input redirection, not much, for example Wc-l < 1.txt statistics out the number of rows of 1.txt
9 | Pipe character, which we have been using, the output of the left command of the pipeline as input to its right command
For example, CAT/ETC/PASSWD |head-3
Let the cat find out the content to the head to re-display
cat/etc/passwd |grep ' root '
Give cat view to grep filter to get target results

Two-Job control

1 Ctrl Z pauses a process
2 BG Throw it in the background run
3 FG restores to foreground and runs
4 jobs view tasks that are paused or running in the background
Command and & can drop the task directly into the background execution
If more than one task is paused, jobs looks at the number of each task
BG, FG back with task number single control one task
5 Ctrl C to end a task

Three variables

If we have a little bit of familiarity with programming, the concept of variables is not unfamiliar, and we usually interpret variables as containers for storing data. Then of course the variables in the shell also mean that. We knew the path before. We can use ENV to view all system variables that are preset by the system, and we can use the SET command to view our custom variables.
The variable format under Linux is: $ variable name
Naming rules for variables: Letters, numbers, underscores, not starting with numbers.
The following focus on the assignment of variables, to see the difference between single and double quotation marks:
Let's briefly cite six examples:
1 a= ' A$BC ' echo $a results A$BC
2.a=1;b=2 echo $a $b results 12
3.a= ' A$BC '; b=2 echo $a $b results A$BC2
4.c= "A$BC" echo $c result A
5.c= "a$b" C echo $c results a2c
6.c= ' a$b ' C echo $c results A$BC
In fact, there are so many examples to illustrate the problem, then we summarize, but the quotation marks will be the contents of the original character as output, do not recognize meaningful symbols. Double quotes will interpret the meaning characters and commands inside. In Example 4, it is interpreted as a BC variable, because the variable does not exist, so it only outputs a
We directly a=111 this to define a variable that will only take effect in the current shell, and if we enter a child shell, it will not be found, then he is a local variable. Local variable
Export a=111 This defines a global variable that the child shell inherits from the parent shell.

Several configuration files for four environment variables:

1 System Layer:
/etc/profile
This file presets several important variables, such as path USER LOGNAME MAIL INPUTRC HOSTNAME histsize umask, etc.
/etc/bashrc
The main presets for this file are Umask and PS1,PS1, which is the string of characters in front of the command we entered.
For example [[email protected]~] [user @ host name current directory] ~ home directory
2 User layer:
. bash_profile
The file defines the user's personalization path and the name of the environment variable. Each user can use the file to enter their own shell information, and when the user logs in, the file executes only once
. BASHRC
The file contains bash information for its own shell, which is read every time a happy shell is played, and the user-defined aliases and variables can be written here.
. bash_history
Used to record command history
. bash_logout
When you exit the shell, the file is executed. You can put the cleanup work here.

Five Special symbols

1.$ We should be impressed with him, when we enter the command under the terminal, we will see that the root user is preceded by a # number, and the normal user is preceded by a $. And we mentioned earlier, as an identifier for a variable.
In a regular expression, as a line-end marker.!$ represents the last variable in the command that was just executed.
For example, performing the following head/etc/passwd tail!$ is equivalent to TAIL/ETC/PASSWD
2~ represents the user's home directory, the root user's home directory is/root, the normal user's home directory is/home/user name
The 3.& command is followed by the symbol, which indicates that the task is dropped to the background for execution
4 semicolons; and && | |
There are three ways to connect two commands:
1 semicolon connection: Indicates execution in sequence, no correlation before and after
2 && Connect: The command before the symbol executes after the command executes successfully. Otherwise, do not execute
3 | | Link: the symbol before the command executes successfully, does not execute after the symbol command, otherwise executes.

Linux Learning Summary (17)-shell Basics

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.