9th Shell Foundation (2) _bash basic functions

Source: Internet
Author: User
Tags aliases clear screen save file

3. Bash 's basic features

3.1 Historical Command and command completion

(1) History command :#history [Options] [history command Save file]

① option:-C: Clears the history command;

-W: Writes the history command in the cache to the file ~/.bash_history

(2) Save the History command: 1000 is saved by default, and the value in the histsize variable of the environment variable configuration file/etc/profile can be modified.

(3) Call of the history command

① using the up and down arrows to invoke previous history commands

② repeating nth history command with "!n"

③ use "!!" Repeat the previous command

④ use "! string" to repeat the last command that starts with the string.

(4) command and document completion

In bash, command and file completion is a very handy and common feature, as long as you enter a command or file, press the "tab" key to automatically complete the completion

3.2 command Aliases and common shortcut keys

3.2.1 Command Aliases

(1) Command aliases:

① set command alias: #alias alias = ' original Command '

② query command aliases: #alias

(2) Order of command execution

  ① 1th bitwise execution of commands executed with absolute or relative paths

② 2nd CIS Execution alias

③ internal command to execute bash in 3rd place

④ 4th executes the first command found in the order in which the directory lookup is defined by the $PATH environment variable.

(3) Make the alias permanent: VI/ROOT/.BASHRC//Let the root user take effect permanently.

(4) Delete aliases: #unalias aliases

3.2.2 Bash Common shortcut keys

Shortcut keys

Role

CTRL + A

Move the cursor to the beginning of the command line. If we enter a command that is too long and want to move the cursor to the beginning of the command line, use

Ctrl+e

Move the cursor to the end of the command line

CTRL + C

Force the current command to terminate

Ctrl+l

Clear screen, equivalent to clear command

Ctrl+u

The command before the cursor is deleted or clipped. Input a long line of command, do not use the BACKSPACE key to delete a character, the use of this shortcut will be more convenient.

Ctrl+k

Delete or cut the contents after the cursor

Ctrl+y

Paste Ctrl+u or ctrl+k-cut content

Ctrl+r

Search in the history command, after pressing Ctrl+r, the search screen will appear, as long as the search content is entered, will be searched from the history command

Ctrl+d

Exit Current Terminal

CTRL + Z

Pause and put in the background. This shortcut is involved in the work management content. In the later section of the system administration, we describe in detail

Ctrl+s

Pause Screen Output

Ctrl+q

Restore Screen Output

3.3 input and output redirection

3.3.1 standard input and output

display

TD width= ">

device

device file name

file descriptor

keyboard

/dev/stdin

0

standard input

/td>
display

/dev/stdout

/td>

1

standard output

/dev/stderr

2

standard error output

3.3.2 change the direction of standard input, standard output, standard error

Type

Operator

Use

REDIRECT Standard input

<

Change the path in the command to receive input from the default keyboard to the specified file

REDIRECT Standard output

> or

1>

Prints the correct result of the execution of the command to the specified file or device, rather than directly on the screen, in the overridden manner.

(1 of which indicates that the correct result is output to the specified file)

>> or

1>>

Outputs the correct result of the command execution to the specified file in an append manner

REDIRECT Standard error

2>

Empties the contents of the specified file and saves the standard error information to the file (where 2 indicates that the command result is wrong, output to the specified file!) )

2>>

Append standard error information to the specified file (note that there are no spaces between 2 and >>)

REDIRECT standard output and standard error

&>

Save all standard output, standard error content to the specified file, rather than directly on the screen (where & shows correct and incorrect results)

Note: The above operators can be combined such as:

#bash hello.sh 1>>a.txt 2>&1 (compared to #bash hello.sh &>> a.txt // The correct or incorrect results that occur when executing the hello.sh script are saved in the A.txt file. )

#ls &>/dev/null // indicates that the result of the command is not needed, just drop the bin. This format can be used to discard some of the execution results that are not concerned.

#bash hello.sh >>a.txt 2>> b.txt // will be correctly saved in A.txt, error results saved in B.txt

3.3.3 examples Show

(1) Statistics file: #wc [options] [filename]

① options:-C: Count bytes,-w: Count Words,-l: Count rows.

3.4 Multi-command sequential execution with pipe character

3.4.1 sequential execution of multiple commands

(1) Multi-command executor

Multi-Command Executor

Format

Role

; (semicolon)

Life 1; Command 2

Multiple commands are executed sequentially, and there is no logical connection between the commands .

such as #ls; Cd/user;date

&&

Command 1 && Command 2

Logic and

Command 2 does 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 does not execute when command 1 is executed incorrectly

Command 2 does not execute when command 1 is executed correctly

(2) Example

① Copy file: #dd if= input file of= output file bs= bytes count= number

where if= input file: Specifies the source file or source device. of= the output file, specifying the destination file or target device. bs= bytes, specifying how many bytes of input/output at a time, that is, the bytes as a block of data. Number of count=, specifying how many data blocks to input/output.

#date, DD if=/dev/zero of=/root/testfile bs=1k count=100000;date//     Create a 100M test file and record the start and end times.

②#./configure && make && make install//only the previous command is correct and the next command will be executed.

③ to determine if the command was executed correctly:   # command && echo yes | | Echo no

3.4.2 Pipe break: |

(1) Format:#命令1 | command 2 // command 1 The correct output as the operand of command 2

(2) Precautions:

① cannot be an operand to command 2 if the command is faulted or has no output result

(3) grep command: #grep [options] "Search Content"

① searches for text using regular expressions in the file and prints the matching lines.

② Options:-I: Ignore case,-N: Output line number,-V reverse lookup,--color=auto search keywords are displayed in color.

(3) Application examples

① split screen display: # Ll-a/etc/| More

② querying all network connections that have been created: #netstat-an | grep "established" or #netstat-an | grep established

3.5 wildcard characters and other special symbols

3.5.1 wildcard characters

(1) Common wildcard characters

Wildcard characters

Role

?

Match an arbitrary character

*

Match 0 or any number of characters, 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] means 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.

(2) Application examples

3.5.2 Bash other special symbols in

(1) Other special symbols

Wildcard characters

Role

‘‘

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

""

Double quotation marks. Special symbols have no special meaning in double quotes, but "$", "" "(anti-quote), and" \ "are exceptions , with the special meaning of" value of the calling variable "," Reference command ", and" escape character ".

``

The inverted quotation marks, located below the ESC key . 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 a shell script, a line that begins with # represents a comment

$

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

\

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 instead of being a variable reference.

(2) Application examples

① single and double quotation marks

#name =sc#Echo '$name'     //Output: $name#Echo "$name"     //output: sc = = "echo $name#Echo  '$ (date)'  //Output: $ (date)#Echo  "$ (date)"  //output current date, such as Saturday, December 31, 2016 00:26:45 CST

② back quotes with $ ()

# echo 'lsecho $ (ls); output ls command result #echo  ls   ==> output:ls

9th Shell Foundation (2) _bash basic functions

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.