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 'ok'; else echo 'no'; 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 ':$' /etc/inittab egrep ‘^hello$' file
9、> greater than sign
Output redirection
##
echo '123' >test.txt 表示将123 输入到文件test.txt中 条件测试中的大于号
11, < less than sign
12, + plus sign
13,>>
echo "123" >> test.txt Append 123 to the file test.txt
##14、<<
here document
For example:
# passwd <<end > 123 > 123 > end
Change the password of user root.
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 -
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
解决变量赋值空格的问题 例如:a='1 2' 阻止shell替换 17、"" 双引号 解决变量赋值空格的问题 例如:a="1 2" 阻止shell部分字符替换,对$、!等无效 18、`` 反引号 相当于 $() 命令行替换 例如:可以设变量a=`ls` 19、% 百分号 算术运算中的模运算 例如: vi中替换操作中表示所有行 (末行模式下,替换所有前面加 %) 例如:在末行模式下输入 :% s/D/d 表示将文本中的所有的D替换为d 20、() 单圆括号 子shell中执行命令,会继承父shell的变量 括起数组元素 例如:定义一个数组 a=(1 2 3 4) 21、(()) 双圆括号 算术运算 例如: 整数比较测试 例如: 22、[] 单方括号 通配符和正则中表示匹配括号中的任意一个字符 例如: 条件测试表达式 例如: 数组中下标括号 例如: 23、[[]] 双方括号 字符串比较测试 例如: 24、. 英文句点号 正则中表示任意1个字符 例如: 当前shell执行脚本命令 例如: 表示当前目录 例如: 25、{} 大括号 通配符扩展 abc{1,2,3} 正则表达式中表示范围 例如:a{3} 匹配3个 a 匿名函数 { } 里面的命令,是在当前shell执行 注意: { } 第一条命令前面要有空格,后面的命令要有分号 括起变量名 26、/ 正斜杠 算术运算中的除法 例如: 根目录或路径分割符 例如: 27、^ 在通配符中表示取反 例如:[^abc] 表示匹配除了abc外的任意一个字符 在正则表达式中表示以什么开头 例如: 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!echo $((100%10))
就是100除以10的余数为0echo $((10/2))
结果就是5(( 10>2 ))
判断10是否大于2 [abc]
表示匹配abc中的任意一个字符[ -f /etc/passwd ]
// 测试是不是文件echo ${a[0]}
表示取数组中下标为0的值[[a=b]]
用来字符串的比较a...b
表示 匹配 a和b之间夹三个字符的字符串./test.sh
执行当前路径下的shell脚本test.shcd ./bgk
进入当前目录下的bgk目录下for i in {1...10}
循环指定范围{ cmd1;cmd2;cmd3;} &> /dev/null
${abc}a
echo $((10/2))
结果就是5cd /usr/local/
表示路径egrep ‘^hello$' file