Home > Operation and Maintenance > Linux Operation and Maintenance > Summary of special characters in Shell

Summary of special characters in Shell

巴扎黑
Release: 2017-08-15 15:55:38
Original
1964 people have browsed it

This article mainly summarizes the relevant information about special characters in Shell. The article includes characters, &, #,! The usage of a series of special characters such as , $, greater than sign, single and double quotation marks, etc. is introduced in great detail through the sample code. It has certain reference learning value for everyone's study or work. Friends who need it can take a look below.

Preface

As we all know, the shell is a command parser for Unix-like operating systems. It is used to interpret and execute a series of commands entered by the user. It is similar to command under DOS and later cmd.exe in Windows. At the same time, the shell is also a programming language. As a command-interpreted scripting language, it interactively interprets and executes commands entered by the user or automatically interprets and executes a series of preset commands; as a programming language, it predefines various environment variables and retains some The meaning of keywords and some special characters, and provides many control structures that are only available in high-level languages, including loops and branch judgments.

This article will introduce to you the relevant content about the usage of Shell special characters. Without further ado, let’s take a look at the detailed introduction:

1. Semicolon

Continuously run commands


# ifdown eth0;ifup eth0
Copy after login

2, | Pipeline

Represented in the regular expression or


# echo "ooooee" |egrep '(oo|ee)'{2} 表示匹配 oooo 或者 eeee 的字符
Copy after login

The standard output of the previous command is used as the standard input of the subsequent command


# ifconfig|grep eth0 表示ifconfig查出来的信息然后过滤出eth0的这一行
Copy after login

3.&

Put the command to the background for execution


# mysqld_safe --user=mysql & 将MySQL放到后台启动
Copy after login

represents standard output and standard error output


# ifconfig &>/dev/null 将ifconfig执行得到的结果输出到/dev/null里面
Copy after login

4, &&

Execute the following command only when the return value of the previous command is 0


# ls && echo "ok"
Copy after login

5, ||

Execute the following command only if the return value of the previous command is non-0


# lls || echo "ok"
Copy after login

6, # pound sign

# represents a comment

$# represents the number of positional parameters


# echo $#
0
Copy after login

${#Variable name} indicates the length of the variable


# a='hello'
# echo ${#a}
5
Copy after login

${#Variable Name[@]} represents the number of arrays


# a=(1 2 3)
# echo ${#a[@]}
3
Copy after login

7,! Exclamation mark

Invert the return value of the command or conditional expression


# if ! [ 1<2 ]; then echo &#39;ok&#39;; else echo &#39;no&#39;; fi
ok
Copy after login

Execute historical command


# history 
1 ls
2 tail test1.txt
3 mysql -uroot -p123
4 ls /tmp/
5 cd /tmp/
[root@localhost ~]# !994
ls /tmp/
account.sql data.sql mysql.sock t1.txt t2.txt
Copy after login

Execute external shell commands in vi or ftp

For example: in vim, if you want to execute a command, just at the end Line mode, enter! After the exclamation point, add the command to be executed

Indirect application variables

For example: ${!a} ---- Indirectly take b Value

8, $ dollar sign

Get the value of the variable


# a=10
# echo $a
10
Copy after login

Regular expression indicates the end of the line


egrep &#39;:$&#39; /etc/inittab 
egrep ‘^hello$&#39; file
Copy after login

9、> greater than sign

Output redirection


##

echo &#39;123&#39; >test.txt 表示将123 输入到文件test.txt中 条件测试中的大于号
Copy after login

11, < less than sign

Input redirection


Less than sign in conditional test


= Equal sign


Variable Assignment - For example: Set variable a=10


Equal sign in conditional test-For example: [ a=b ] Determine whether variable a is equal to b


Numerical comparison== - For example: (( a==20 )) Determine whether variable a is equal to 20


12, + plus sign

The plus sign in arithmetic operations-for example: 1+3


1 or more preceding characters in regular expressions-for example: ab+c means matching 1 between ab and c or more characters


13,>>

Output redirection append-for example:

echo "123" >> test.txt Append 123 to the file test.txt

##14、<<
here document


For example:


##
# passwd <<end
> 123
> 123
> end
Copy after login

Change the password of user root.


15. - Minus sign

The minus sign in arithmetic operations - For example: 10-2
Options for the

command - For example: ls -l

Last working directory - For example: cd -

Wildcards and regular expressions represent ranges - For example: [a-z]


tar -cvf - /home | tar -xvf -
Copy after login

represents the output stream or input stream

passes the previous output to the latter through the pipeline The command, compression in the front, decompression in the back


16, '' single quote

解决变量赋值空格的问题

例如:a='1 2'

阻止shell替换

17、"" 双引号

解决变量赋值空格的问题

例如:a="1 2"

阻止shell部分字符替换,对$、!等无效

18、`` 反引号 相当于 $()

命令行替换

例如:可以设变量a=`ls`

19、% 百分号

算术运算中的模运算

例如:echo $((100%10)) 就是100除以10的余数为0

vi中替换操作中表示所有行 (末行模式下,替换所有前面加 %)

例如:在末行模式下输入 :% s/D/d 表示将文本中的所有的D替换为d

20、() 单圆括号

子shell中执行命令,会继承父shell的变量

括起数组元素

例如:定义一个数组 a=(1 2 3 4)

21、(()) 双圆括号

算术运算

例如: echo $((10/2)) 结果就是5

整数比较测试

例如: (( 10>2 )) 判断10是否大于2

22、[] 单方括号

通配符和正则中表示匹配括号中的任意一个字符

例如: [abc] 表示匹配abc中的任意一个字符

条件测试表达式

例如: [ -f /etc/passwd ] // 测试是不是文件

数组中下标括号

例如:echo ${a[0]} 表示取数组中下标为0的值

23、[[]] 双方括号

字符串比较测试

例如: [[a=b]] 用来字符串的比较

24、. 英文句点号

正则中表示任意1个字符

例如:a...b 表示 匹配 a和b之间夹三个字符的字符串

当前shell执行脚本命令

例如: ./test.sh 执行当前路径下的shell脚本test.sh

表示当前目录

例如:cd ./bgk 进入当前目录下的bgk目录下

25、{} 大括号

通配符扩展 abc{1,2,3}

正则表达式中表示范围

例如:a{3} 匹配3个 a

for i in {1...10} 循环指定范围

匿名函数{ cmd1;cmd2;cmd3;} &> /dev/null

{ } 里面的命令,是在当前shell执行

注意: { } 第一条命令前面要有空格,后面的命令要有分号

括起变量名 ${abc}a

26、/ 正斜杠

算术运算中的除法

例如:echo $((10/2)) 结果就是5

根目录或路径分割符

例如:cd /usr/local/ 表示路径

27、^

在通配符中表示取反

例如:[^abc] 表示匹配除了abc外的任意一个字符

在正则表达式中表示以什么开头

例如:


egrep ‘^hello$&#39; file
Copy after login

The above is the detailed content of Summary of special characters in Shell. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template