search

In Linux, the pipe character is "|", which is mainly used to connect two or more commands together and use the output of one command as the input of the next command; the syntax is "command1 | command2 [ | commandN... ]", the output of the command on the left side of the "|" character will be used as the input of the command on the right side of the "|" character. The pipe character can be used continuously. The output of the first command will be used as the input of the second command, and the output of the second command will be used as the input of the third command, and so on.

What is the pipe character in linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

Shell also has a function, which is to connect two or more commands (programs or processes) together, and use the output of one command as the input of the next command. The two connected in this way Or multiple commands form a pipeline.

Linux pipes use vertical bars| to connect multiple commands, which is called the pipe character.

command1 | command2
command1 | command2 [ | commandN... ]

When a pipe is set between two commands, the output of the command on the left of the pipe symbol | becomes the input of the command on the right. As long as the first command writes to standard output and the second command reads from standard input, the two commands can form a pipe. Most Linux commands can be used to form pipes.

The pipe character can be used continuously. The output of the first command will be used as the input of the second command, and the output of the second command will be used as the input of the third command, and so on.

What is the pipe character in linux

It should be noted here that command1 must have correct output, and command2 must be able to process the output result of command2; and command2 can only process the correct output result of command1, not Handle the error message of command1.

For example: sort the hello.sh file and find the line containing "better" after sorting and deduplicating it.

The command is: cat hello.sh | sort | uniq | grep 'better'

  • View text
  • Sort
  • Deduplication
  • Filter
    What is the pipe character in linux

[1] The first step - view the text

First use the cat command to view the text, and the content printed on the screen is the output result of the cat command

[root@linuxforliuhj test]# cat hello.sh 
hello this is linux
be better
be better
i am lhj
hello this is linux
i am lhj
i am lhj
be better
i am lhj
have a nice day
have a nice day
hello this is linux
hello this is linux
have a nice day
zzzzzzzzzzzzzz
dddddddd
gggggggggggggggggggg
[root@linuxforliuhj test]#

[2] The second process - sorting

Throw the results output by the previous cat command to the sort command through the pipeline, so the sort command sorts the text output by the previous cat command

[root@linuxforliuhj test]# cat hello.sh | sort
be better
be better
be better
dddddddd
gggggggggggggggggggg
have a nice day
have a nice day
have a nice day
hello this is linux
hello this is linux
hello this is linux
hello this is linux
i am lhj
i am lhj
i am lhj
i am lhj
zzzzzzzzzzzzzz
[root@linuxforliuhj test]#

[3] The third process - deduplication

As mentioned in the previous article introducing uniq, sort can be effectively deduplicated when used in combination with uniq, so sort is processed through the pipeline and output The text is thrown to uniq for processing, so uniq processes the sorted text, which can effectively remove duplicates

[root@linuxforliuhj test]# cat hello.sh | sort | uniq
be better
dddddddd
gggggggggggggggggggg
have a nice day
hello this is linux
i am lhj
zzzzzzzzzzzzzz
[root@linuxforliuhj test]#

[4] The fourth step - filtering

The last step of filtering is the same It is to filter the text output after processing the previous command, that is, the uniq command.

[root@linuxforliuhj test]# cat hello.sh | sort | uniq | grep 'better'
be better
[root@linuxforliuhj test]#

Here comes the important point!

Here comes the important point!

Here’s the point!

The above cat, sort, uniq, grep and other commands all support the pipe character because these commands can be read from the standard input. The text to be processed (that is, reading parameters from the standard input); for some commands, such as rm, kill and other commands, do not support reading parameters from the standard input, but only support reading parameters from the command line (that is, the rm command The file or directory to be deleted must be specified later, and the process number to be killed must be specified after the kill command.)

So what kind of commands support pipes, and what kind of commands do not support pipes? ?
Generally, commands that process text, such as sort, uniq, grep, awk, sed, etc., all support pipes; commands that do not process text, such as rm and ls, do not support pipes.

[root@linuxforliuhj test]# cat hello.sh | sort
be better
be better
be better
dddddddd
gggggggggggggggggggg
have a nice day
have a nice day
have a nice day
hello this is linux
hello this is linux
hello this is linux
hello this is linux
i am lhj
i am lhj
i am lhj
i am lhj
zzzzzzzzzzzzzz
[root@linuxforliuhj test]#

When there are no parameters after sort, the output of the previous command thrown to it by the pipe character is processed (that is, the standard output of the previous command is used as the standard input of this command)

[root@linuxforliuhj test]# ls
beifen.txt  hello.sh  mk  read.ln  read.sh  read.txt  sub.sh
[root@linuxforliuhj test]# ls | grep read.sh
read.sh
[root@linuxforliuhj test]# ls | grep read.sh | rm
rm: missing operand
Try 'rm --help' for more information.
[root@linuxforliuhj test]#

When If the deleted file is not specified after rm, an error will be reported as missing parameters. Therefore, commands such as rm do not support reading parameters from the standard input. They only support specifying parameters on the command line, that is, specifying the deleted file.

Which takes precedence between standard input and command line parameters?

There are the following two files

[root@linuxforliuhj test]# cat a.txt 
aaaa
dddd
cccc
bbbb
[root@linuxforliuhj test]# cat b.txt 
1111
3333
4444
2222
[root@linuxforliuhj test]#

Execute the command: cat a.txt | sort

[root@linuxforliuhj test]# cat a.txt | sort
aaaa
bbbb
cccc
dddd
[root@linuxforliuhj test]#

When the command line parameter of sort is empty, the former is used by default The output result of a command is used as the input of this command.

Execute the command: cat a.txt | sort b.txt

[root@linuxforliuhj test]# cat a.txt | sort b.txt 
1111
2222
3333
4444
[root@linuxforliuhj test]#

You can see that when the command line parameters of sort (here are b.txt) is not empty, sort will not read the parameters in the standard input, but will read the command line parameters

Execute the command: cat a.txt | sort b.txt -

[root@linuxforliuhj test]# cat a.txt | sort b.txt -
1111
2222
3333
4444
aaaa
bbbb
cccc
dddd
[root@linuxforliuhj test]#

" - "means standard input, that is, the output of the command cat a.txt, which is equivalent to sorting the file b.txt and standard input together, which is equivalent to sort a.txt b.txt

[root@linuxforliuhj test]# sort a.txt b.txt
1111
2222
3333
4444
aaaa
bbbb
cccc
dddd
[root@linuxforliuhj test]#

思考:对于rm、kill等命令,我们写脚本时常常会遇到需要查询某个进程的进程号然后杀掉该进程,查找某个文件然后删除它这样的需求,该怎么办呢?那就用xargs吧!

相关推荐:《Linux视频教程

The above is the detailed content of What is the pipe character in linux. 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

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.