search

Summary of special characters in Shell

Aug 15, 2017 pm 03:55 PM
shellcharacter

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,

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

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 -

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

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
Linux: Entering and Exiting Maintenance ModeLinux: Entering and Exiting Maintenance ModeMay 02, 2025 am 12:01 AM

The methods to enter Linux maintenance mode include: 1. Edit the GRUB configuration file, add "single" or "1" parameters and update the GRUB configuration; 2. Edit the startup parameters in the GRUB menu, add "single" or "1". Exit maintenance mode only requires restarting the system. With these steps, you can quickly enter maintenance mode when needed and exit safely, ensuring system stability and security.

Understanding Linux: The Core Components DefinedUnderstanding Linux: The Core Components DefinedMay 01, 2025 am 12:19 AM

The core components of Linux include kernel, shell, file system, process management and memory management. 1) Kernel management system resources, 2) shell provides user interaction interface, 3) file system supports multiple formats, 4) Process management is implemented through system calls such as fork, and 5) memory management uses virtual memory technology.

The Building Blocks of Linux: Key Components ExplainedThe Building Blocks of Linux: Key Components ExplainedApr 30, 2025 am 12:26 AM

The core components of the Linux system include the kernel, file system, and user space. 1. The kernel manages hardware resources and provides basic services. 2. The file system is responsible for data storage and organization. 3. Run user programs and services in the user space.

Using Maintenance Mode: Troubleshooting and Repairing LinuxUsing Maintenance Mode: Troubleshooting and Repairing LinuxApr 29, 2025 am 12:28 AM

Maintenance mode is a special operating level entered in Linux systems through single-user mode or rescue mode, and is used for system maintenance and repair. 1. Enter maintenance mode and use the command "sudosystemctlisolaterscue.target". 2. In maintenance mode, you can check and repair the file system and use the command "fsck/dev/sda1". 3. Advanced usage includes resetting the root user password, mounting the file system in read and write mode and editing the password file.

Linux Maintenance Mode: Understanding the PurposeLinux Maintenance Mode: Understanding the PurposeApr 28, 2025 am 12:01 AM

Maintenance mode is used for system maintenance and repair, allowing administrators to work in a simplified environment. 1. System Repair: Repair corrupt file system and boot loader. 2. Password reset: reset the root user password. 3. Package management: Install, update or delete software packages. By modifying the GRUB configuration or entering maintenance mode with specific keys, you can safely exit after performing maintenance tasks.

Linux Operations: Networking and Network ConfigurationLinux Operations: Networking and Network ConfigurationApr 27, 2025 am 12:09 AM

Linux network configuration can be completed through the following steps: 1. Configure the network interface, use the ip command to temporarily set or edit the configuration file persistence settings. 2. Set up a static IP, suitable for devices that require a fixed IP. 3. Manage the firewall and use the iptables or firewalld tools to control network traffic.

Maintenance Mode in Linux: A System Administrator's GuideMaintenance Mode in Linux: A System Administrator's GuideApr 26, 2025 am 12:20 AM

Maintenance mode plays a key role in Linux system management, helping to repair, upgrade and configuration changes. 1. Enter maintenance mode. You can select it through the GRUB menu or use the command "sudosystemctlisolaterscue.target". 2. In maintenance mode, you can perform file system repair and system update operations. 3. Advanced usage includes tasks such as resetting the root password. 4. Common errors such as not being able to enter maintenance mode or mount the file system, can be fixed by checking the GRUB configuration and using the fsck command.

Maintenance Mode in Linux: When and Why to Use ItMaintenance Mode in Linux: When and Why to Use ItApr 25, 2025 am 12:15 AM

The timing and reasons for using Linux maintenance mode: 1) When the system starts up, 2) When performing major system updates or upgrades, 3) When performing file system maintenance. Maintenance mode provides a safe and controlled environment, ensuring operational safety and efficiency, reducing impact on users, and enhancing system security.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.