Fundamentals of shell programming for Linux learning articles

Source: Internet
Author: User

Set Environment variables:

①export variable name = variable Value #设置环境变量

② variable name = variable Value #设置环境变量

Export variable Name

Set #显示当前shell所有变量

Env #查看环境变量

unset variable name #删除变量


Common environment variables:

Path variable: Paths to System lookup commands

Echo $PATH #查看PATH的值

Path= "$PATH":/root/sh #将/root/sh directory added to PATH variable (temporarily in effect)


PS1 variable: Command prompt variable


Current language query:

Locale #查询当前语系 (en_US. Utf-8/zh_cn. UTF-8)

LANG: Defining variables for system subject lines

Lc_all: Defining variables for the whole family language

/etc/sysconfig/i18n #语系配置文件


Positional parameter variables:

$n #n为数字, $ A represents the command itself, $1-$9 represents the first to the Nineth parameter, and more than 10 of the parameters require braces, such as ${10}

$* #这个变量代表命令行中所有参数, $* all the parameters as a whole.

[Email protected] #这个变量也代表命令行所有参数, but [email protected] differentiate each parameter

$# #这个变量代表命令行所有参数个数


Pre-defined variables:

$? #最后一次执行的命令的返回状态, the previous command correctly executes a variable value of 0, the previous command executed an incorrect variable value of 0

$$ #当前进程的进程号 (PID)

$! #后台运行的最后一个进程的进程号 (PID)


Receive keyboard input:

read [options] [variable name]

-P: Prompt information

-T: Number of seconds, specifying the wait time

-N: The number of characters that will be executed when the specified number of characters is received

-S: Hide input data, apply to confidential information input

Read-p "Please input your name:"-T-Name

Echo $name


Declare declaring variable types:

Declare [+/-][option] Variable name

-: Set type attribute to variable

+: Cancels the Type property of a variable

-A: Declaring a variable as an array

-I: Declaring a variable as an integer

-x: Declaring a variable as an environment variable

-r: Declaring variables as read-only variables

-P: Displays the declared type of the specified variable


Numeric operations:

①aa=11;bb=22;dd=$ (expr $aa + $bb); Echo $dd #dd为33

②aa=11;bb=22;dd=$ (($aa + $bb)); Echo $dd #dd为33

③aa=11;bb=22;dd=$[$aa + $bb];echo $dd #dd为33


Environment variable configuration file:

~/.bash_logout #注销时执行的配置文件

~/.bash_history #历史命令保存文件

/etc/issue #本地终端登录信息显示文件

/ETC/MOTD #本地登录, Telnet, the contents of this file will be displayed only if the login succeeds


Regular Expressions:

Regular expressions are used to match qualifying strings in a file, while regular expressions include matches, grep, awk, sed, and other commands that can support regular expression

Wildcards are used to match eligible file names, wildcards are exact matches, LS, find, CP, these commands do not support regular, wildcards are available

*: Previous character matches 0 or more times

.: Match any character except line break

^: Matches the beginning of the line, for example ^hello matches lines beginning with Hello

$: Match end of line

[]: Matches any one of the characters in parentheses, matching only one character

[^]: matches any character except for the brackets

\{n\}: Indicates that the preceding character appears exactly n times, for example [0-9]\{4\} matches 4 digits

\{n,\}: Indicates that the preceding character appears not less than n times, for example [0-9]\{2,\} represents two digits and above

\{n,m\}: Indicates that the preceding character appears at least n times, up to M times [a-z]\{6,8\} matches lowercase letters from 6 to 8 bits


Character intercept command:

Cut command:

Cut [Options] file name

-F Column Number: Extract the first few columns

-D delimiter: Split columns by the specified delimiter (Default Delimiter Tab key)

grep "/bin/bash"/etc/passwd | Grep-v "Root" | Cut-f 1-d ":" #查询系统中的普通用户

Cut-d ":"-F 1,3/ETC/PASSWD


awk command:

awk ' Condition 1{action 1} Condition 2{action 2} ... ' Filename

df-h | awk ' {print $ ' \ t ' $ $} ' #截取第一和第三列

cat/etc/passwd | Grep/bin/bash | awk ' begin{fs= ': "}{print" \ T "$"

#FS为指定分割符


sed command: (append, insert, replace, delete, display the file)

Character Processing commands:

Sort [Options] file name #排序命令

-F: Ignore case

-N: Sort by numeric type, by default using string type sorting

-R: Reverse Sort

-T: Specifies the delimiter, which is a tab character by default

-K n[,m] : Sort by the specified field range, starting with the nth field and ending with the M field (default to end of line)

Sort-n-T ":"-K 3,3/etc/passwd #按照第3个字段以数值型排序

WC [option] File name

-L: Count rows only

-W: Counts only the number of words

-M: Count only characters

Wc-l/etc/passwd #统计行数


This article is from the "we_need_to_try" blog, make sure to keep this source http://liuxiao255.blog.51cto.com/10551403/1835007

Fundamentals of shell programming for Linux learning articles

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.