The Shell Foundation of Linux

Source: Internet
Author: User
Tags aliases clear screen control characters delete key save file

Shell Basics

First, Shell Overview

1) The Shell is a command-line interpreter that provides the user with an interface system-level program that sends a request to the Linux kernel to run the program, allowing the user to start, suspend, stop, or even write programs with the shell.

The ABC 、、、 24 characters we entered are translated into binary by the shell against the ASCII code to allow the computer to recognize. We enter the command from the operator interface, this command returns to the command after the ASCII code translation into the specified binary, through the shell translated into the computer nuclear power recognition of the binary, and then the kernel calls the hardware to process, after processing and then through the shell feedback to the user.

2) Shell is also a very powerful programming language, easy to write, easy to debug, flexible. The shell is the scripting language that interprets execution, and the Linux system commands can be invoked directly in the shell.

Second, How scripts are executed

1) echo Output Command

1. Echo [options] [Output CONTENT]

For example: [[email protected] ~]# echo "Hello World" this will print. Double quotation marks are required if there are spaces between the contents of your output.

Note: options:

-E: Character conversions that support backslash control

Control characters

Role

\a

Output warning tone

\b

Backspace key, that is, delete key to the left

\ n

Line break

\ r

Enter

\ t

tab, which is the TAB key

\v

Vertical tab

\0nnn

Output characters according to the Octal ASCII code table. Where 0 is a digital zero and NNN is a three-bit octal number

\xhh

Output characters according to hexadecimal ASCII, where HH is a two-bit hexadecimal number

For example: [[email protected] ~]# echo "hello\b World" The result of the output is: Hell World

2. echo Color Output

For example: [[email protected] ~]# echo–e "\e[1;31m Hello World \e[0m" The result of the output is: Hello World

#30m = black, 31m= red, 32m= green, 33m= Yellow

#34m = blue, 35m= magenta, 36m= cyan, 37m= White

2) First Script

[Email protected] ~]# vim hello.sh

#!/bin/bash (indispensable, not omitted)

#这是第一个shell脚本

Echo–e "\e[1;34m in the sky to the next Lin sister \e[0m"

Press ESC to go back to global mode, then press: Wq Save to exit

3) Execute script

1, give the executive permission to run directly

Chmod 755 hello.sh (Grant permission)

./hello.sh (relative path invocation) or

/root/hello.sh (absolute path invocation)

2. Execute script via bash call

Bash hello.sh

Third, basic features of Bash

1) command aliases and shortcut keys

1. alias (View all command aliases in the system)

For example: [[email protected] ~]# ls

is actually ls= ' ls--color=auto '

2. Alias Set name = ' Original command is alias ' (set command alias)

3. aliases are permanently active and delete aliases

VI ~/.BASHRC (log in after the environment variable configuration file is in effect or execute source. BASHRC)

Unalias aliases (remove aliases)

4. Command Effective order

First-bit execution of commands executed with absolute or relative paths

Second cis-execute alias

Third-level execution of bash internal commands

The first command found in the directory lookup order defined by the $PATH environment variable is executed in the second

5. Common shortcut keys

CTRL + C Force terminate current command ctrl+l clear screen

CTRL + A cursor moves to the beginning of the command line ctrl+e the cursor to the end of the command

Ctrl+u from the cursor position to the beginning of the line CTRL + Z place commands in the background

Ctrl+r Search in the history command

2) Historical Command

History [Options] [Historical command Save file]

Option:-C: Empty history command

-W: Write History command in cache to history command to save file ~/.bash_history

1, the history command will save 1000 by default, can be modified in the environment variable configuration file/etc/profile

2. Call of the history command

Use the up and down arrows to invoke previous history commands

Use "! n "Repeating nth history command

Use "!! "Repeatedly executes the previous command

Use "! String "repeats the last command that starts with the string

3. command and file completion

In bash, command and file completion are very convenient and common functions, as long as we enter a command or file, the TAB key will automatically complete the completion

3) Output Redirection

1. Standard input and output

Equipment

Device file name

File descriptor

Type

Keyboard

/dev/stdin

0

Standard input

Display

/dev/sdtout

1

Standard output

Display

/dev/sdterr

2

Standard error Output

All files in Linux

Definition: The information that the original command wants to display on the screen through >,>> output to the file

2. Output redirection

Type

Symbol

Role

Standard output redirection

Commands > Documents

To export the correct output of the command to a specified file or device in a covered manner

Commands >> Documents

Output the correct output of the command to the specified file or device in an additional way

Standard Error Output redirection

Error command 2> file

Output the error output of the command to the specified file or device in a covered manner

Error command 2>> file

Output the error output of the command to the specified file or device in an additional way

Note: The standard output redirection command >, >> can be a blank space or a line.

2 and redirect symbol (>, >>) no space between

Correct output and error output are saved simultaneously (2, 4, 5 more commonly used, 2, 4 functions as well)

Commands > Documents 2>&1

Save the correct output and error output to the same file in a covered manner

Commands >> Documents 2>&1

Save the correct output and error output to the same file in an additional way

Commands &> Documents

Save the correct output and error output to the same file in a covered manner

Commands &>> Documents

Save the correct output and error output to the same file in an additional way

Commands >> files 1 2>> file 2

Append the correct output to the file 1 and append the wrong output to file 2

Note: Command &>/dev/null (Black hole in Linux, as long as the throw in will not appear directly discarded)

[[email protected] ~]# WC [option] [filename]

Options:

-C Statistics byte number

-W statistical number of words

-L count rows

Command < file (file as input to the command)

Commands << identifiers

......

Identifier (the input of the contents between identifiers as commands)

4) sequential execution of multiple commands

Multi-Command Executor

Format

Role

Command 1; command 2

Multiple command order execution, no logical connection between commands

&&

Command 1&& Command 2

Logic and

Command 2 will not execute when command 1 is executed correctly;

Command 2 does not execute when command 1 is not executed correctly

||

Command 1| | Command 2

Logical OR

Command 2 will not execute when command 1 is executed incorrectly;

Command 2 does not execute when command 1 is executed correctly

For example: [[email protected] ~]# Pwd;touch abc;ls;date

[[email protected] ~]# ls anaconda-ks.cfg&& echo Yes

[[email protected] ~]# ls anaconda-ks.cfg&& echo No

[[Email protected] ~]# command && echo yes | | Echo No

5) Pipe character

Command format: [[email protected] ~]# Command 1 | Command 2

(the correct output of command 1 is the Action object of command 2)

For example:

[Email protected] ~]# ll–a/etc/| More

[Email protected] ~]# Netstat–an | grep "established"

6) Other special symbols in bash

Symbol

Role

‘’

Single quotation marks. All special symbols in single quotes, such as "$" and "'" (anti-quotes), have no special meaning.

“”

Double quotation marks. Special symbols do not have special meanings in double quotes, but "$", "" "and" \ "are exceptions, with the special meanings of" value of the calling variable "," Reference command ", and" escape character ".

``

Anti-quote. The contents of the inverted quotation mark are system commands, which are executed first in bash. As with $ (), it is recommended to use $ () because the anti-quote is very easy to read.

$()

As with the anti-quote function, used to refer to system commands.

#

In the shell script, #开头的行代表注释

$

The value that is used to invoke the variable, such as when the value of the variable name needs to be called, the value of the variable needs to be $name

\

The escape character, followed by a special symbol after \ will lose its special meaning and become a normal character. such as \$ will output the "$" symbol, not as a variable reference

7) wildcard characters

Wildcard characters

Role

Match an arbitrary character

*

Match 0 or any number of any character, which can match anything

[]

Matches any one of the characters in the brackets. For example: [ABC] must match a character, or a or B, or C

[-]

Matches any one of the characters in the brackets-representing a range. For example: [A-z] stands for matching a lowercase letter

[^]

Logical non, which means that the match is not a character within the brackets. For example [^0-9] stands for matching a character that is not a number

The Shell Foundation of Linux

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.