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

Summary of special characters in Shell

巴扎黑
巴扎黑Original
2017-08-15 15:55:381909browse

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

2, | Pipeline

Represented in the regular expression or


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

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


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

3.&

Put the command to the background for execution


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

represents standard output and standard error output


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

4, &&

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


# ls && echo "ok"

5, ||

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


# lls || echo "ok"

6, # pound sign

# represents a comment

$# represents the number of positional parameters


# echo $#
0

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


# a='hello'
# echo ${#a}
5

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


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

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

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

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

Regular expression indicates the end of the line


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

9、> greater than sign

Output redirection


##

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

11, cb8b2ca35ea70db8cc8d1ee11422297a>

Output redirection append-for example:

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

##14、ceae7b6e3899046ecc44e6c03f621a9d2 )) 判断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

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!

Statement:
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