Linux Shell Learning

Source: Internet
Author: User
Tags echo command telnet program

Shell Features--

Linux has a variety of shell can be used, the default for Bash,bash has the following key features.

1. Command Memory Ability

Press the up and down keys on the command line to find a pre/Post input command, which is recorded in ~/.bash_history, which records all commands previously executed by the last login shell, and the commands executed by the current login shell are temporarily in temporary memory. These commands are updated to the file after the system is successfully unregistered. Historical commands can be viewed through the history command and can be combined with an exclamation mark when executing a history command again! Use together, the exclamation mark followed by the sequence number of the history command. If multiple sign-in bash, which opens several bash windows, the last bash that was logged out will overwrite the previous bash's history.

2. Tab key

When a partial command or file is not fully entered, press the TAB key once to complete the command or file, and press the TAB key two times to list all available commands or files.

3. Command alias setting function

Use alias to set a simple alias for a longer command, Unalias cancel the setting, as in the following example:

$alias lm=‘ls -alt‘

The alias command above is only valid for the current bash, and the alias will be invalidated after bash exits, how can it be set once to be valid in all bash forever? This is done by writing the alias command above to the ~/.BASHRC file, which is read into the shell using the source command, using a period, or restarting the computer.

4. Built-in command type

By using the type command, we can know the type of a command, whether it is a built-in command or an external command or an alias, and the type command itself is a built-in command. Type commonly used three parameters,-t display command type,-P only when the command type is an external command to display the command full path,-a displays all command-related instruction information in the PATH environment variable, and also includes the alias alias command.

5. Command execution

Command execution requires pressing the ENTER key, and if the command requires multiple lines of input, it can be escaped with a \ character immediately before the ENTER key, with no spaces between them.

6. Bash Environment

1) path and command lookup have a certain order, their order is: relative or absolute path execution command >alias command >builtin command > through $PATH environment variable order to find the first command.

2) We can customize the Linux login and welcome information, the configuration files are/etc/issue and/ETC/MOTD, and/etc/issue.net is provided to Telnet for this Telnet program.

Shell Variables--

As with other programming languages, you can customize variables in the shell, but there are certain rules that are summarized below.

1, such as the common environment variable path, the display of the variable can be viewed through the echo command, before the variable to add the $ symbol, the variable can be optionally used in a pair of curly braces.

2. The variable is assigned by equal sign = no space on either side of the equal sign.

3, the variable name can only be English or numeric, the beginning character cannot be a number.

4, the variable content contains special characters, the escape character can be escaped, the special character inside the double quotation mark such as $ to keep the original characteristic, the special word in single quotation mark such as $ is a common character, the quotation marks appear in pairs, and match from left to right.

5. When other commands are included in a string of commands, other commands can use the inverted single quote ' command ' or the dollar sign combined with the parentheses $ (command), which is executed first, and the result is executed as input information.

6. Use the "$ Variable name" or the "{ variable name}" action when adding content to the variable, as follows:

$PATH=‘‘$PATH‘‘xxx$PATH=${PATH}xxx

7. Variables need to export this variable when the child process is in effect, making it an environment variable. You can use the Bash command to enter a child process and use the Exit command when exiting.

8. Use unset when canceling variables.

9. Environment variable view You can use the ENV command, or you can use the Export command to view all variables, including environment variables and custom variables, from the SET command.

10. The declare command is used to declare variable types, similar to typeset,-a for arrays,-I for integers, and-X is similar to EXPORT,-R for specifying variable read-only.

11, a few special variables: PS1 for the command prompt, PS2 for the use of the escape character after the second line of the command prompt,$ for the shell of the PID, the callback code for the last execution of the command, the callback code of 0 indicates that the command executed successfully.

12, the Read command can read the keyboard input to the variable, the-p parameter can specify the prompt information, for example:

$read-p‘‘please input your name:‘‘ name

13. The Ulimit command can set the user's restrictions on system resource usage.

14, the deletion of variable content can use #, # #,%, percent of the symbol, the content can be replaced with/,//symbol.

${变量#关键词}——从左向右匹配,删除最短数据。${变量##关键词}——从左向右匹配,删除最长数据。${变量%关键词}——从右向左匹配,删除最短数据。${变量%%关键词}——从右向左匹配,删除最长数据。${变量/旧字符串/新字符串}——从左向右匹配,删除第一个匹配的数据。${变量//旧字符串/新字符串}——从左向右匹配,删除所有匹配的数据。

15, variable value testing and replacement can be used-, +, = 、?、: Symbols, etc., the effect is as follows:

Common key combinations--

Ctrl + C: Terminates the current command.
Ctrl + D: Enter end EOF to exit the current shell.
CTRL + M:enter key.
Ctrl + S: Pauses the output of the screen.
Ctrl + Q: Reply to the output of the screen.
Ctrl + U: Removes the entire line of commands under the prompt character.
Ctrl + Z: Pauses the current command.

Common wildcard characters and special symbols--
*:任意数目个任意字符。?:任意一个字符。[abcd]:任意一个方括号内的字符,即a、b、c、d中任意一个。[a-z]:任意一个从a到z范围内的字符,关键在于中间的减号,表示范围。[^abc]:^代表反向选择,即除了abc之外,任意一个其它的字符。#:注释符号。\:转义字符。|:管线。;:分号,连续几个命令间的分隔符。~:用户主目录。$:用于读取变量。&:工作控制,当前命令后台作业。!:逻辑非。/:路径分隔符。>、>>:数据流重定向,输出,分别是替换和追加。2>、2>>:数据流重定向,错误输出,数字2与尖括号之间没有空格。<、<<:数据流重定向,输入。‘ ‘:单引号,上面提到了,不具有变量替换功能。‘‘‘‘:双引号,具有变量替换功能` `:符号之间为可执行命令,等同于$()。( ):相当于子shell。{ }:命令区块的组合。
Examples of data flow redirection usage--

stdin, stdout, stderr is actually three special files, the file identifiers are 0, 1, 2, the following example uses these numbers.
To copy the contents of a plain text file from the to file to, there are two ways to use data flow redirection:

fromtotofrom

Sometimes when executing a command, there are standard output, standard error output, and standard output redirected to file:

$command > file

REDIRECT the standard error output to file:

$command 2> file

There are three ways to redirect standard output and label error output to file files at the same time:

$command > file 2>&1$command 2> file >&2$command &> file

Notice the use of the numbers above.
/dev/null is a special file that can be understood as an infinitely large dumpster that redirects any unwanted data to this file.

The command line executes several commands consecutively--
Method One: Use semicolons

$command1; command2

Method two: Using && and | | Symbol

$command1 && command2$command1 || command2

&& | | The short-circuit symbol that is commonly referred to.

Pipeline Command--

1. Pipeline

Pipeline is a vertical line |, the command on the left to have standard output, and only handle standard output, standard error is ignored, the right command to accept the standard input, the following describes a few common pipeline commands.

2. Cut

Intercept part of the data in a behavioral unit.

$echo$PATH-d‘:‘-f1

In the PATH environment variable,-d specifies that the delimiter is a colon, the colon separates the contents of the PATH environment variable, and-f specifies the first field after the split, where the 1th field, and optionally multiple fields, are separated by commas. -d specifies that the delimiter can be a space, such as the following command:

$last|-d‘ ‘-f1$export|-c10-20

-C intercepts the 10th to 20th characters of each line.

3. grep

Matches the target string in the behavior unit, and then outputs the entire line of content, such as:

$topgrep init

Here are a few common parameters:
-C: Calculates the number of string matches.
-I: Ignore case.
-N: Output line number.
-V: Reverse selection, that is, the output does not match the line to the string.
Grep can also be used alone, in the following format:

<string><filename>

4. Sort

Sort is sorted, such as:

$lssort -r

-R indicates reverse ordering.

5, Uniq

Uniq is used to process duplicate data, filtered only once, such as:

$last|-d‘ ‘-f1|-c

Displays the total number of times each account was logged in.

6. WC

How many lines, strings, and characters are in the WC statistics text, such as:

$wc filename

Where-L can count the number of rows, the-w statistic string, and the-C statistic character.

7. Tee

As you can see from the previous commands, the data stream is redirected to a file, and the screen is not output, and the tee can be directed in both directions, side-by-side to the file, and to the screen, such as:

$ls | tee filename

The results of LS are redirected to the filename and also output to the screen.

8, TR

TR is used to delete or replace strings.

$ls|[a-z] [A-Z]

As the example above, TR replaces all lowercase letters in the results of LS to uppercase.

$ls-d‘string‘

The-d parameter specifies the string to delete.

9. Col

Col Filters some data, options and parameters:
-X: Converts the TAB key to the equivalent space bar.
-B: Only the last character of the backslash is preserved when there is a backslash/backslash in the file.

10. Join

Merge two rows of data into one row, in the following format:

$join [options] file1 file2

The options and parameters are as follows:

-t <string>:join默认以空格符分隔数据,-t指定具体的分割符,并且在file1和file2中查找string,查找成功时,把file1和file2的两行数据连成一行,且string只显示一次,在行首显示。-i:忽略大小写。-1 <num>:数字1,指定第一个文档file1要用哪个字段来分析。-2 <num>:数字2,指定第二个文档file2要用哪个字段来分析。

11, paste

Merge multiple lines of text into a single line, with the TAB key as the default, with the syntax in the following format:

$paste[options][files]

The common parameter is-D, which specifies the delimiter.

12, expand

Expand converts the TAB key to a space.

13. Split

Separate large files into smaller files with the following syntax:

$split[options][file]PREFIX

The common option is-B, specify a small file size, and the-l parameter specifies the number of small file rows. Prefix is a small file prefix that can optionally be specified as a custom string.

14, Xargs

Xargs reads the standard input, separates it with whitespace or newline characters, separates the standard input into multiple parameters, and executes the specified command, in the following format:

command

For example, the following command looks for a core-named file in the/tmp directory,

$find-name-type-print0|-0-f

..........

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux Shell Learning

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.