Table of Contents
Which takes precedence between standard input and command line parameters?
Home Operation and Maintenance Linux Operation and Maintenance What is the pipe character in linux

What is the pipe character in linux

Jun 17, 2022 pm 03:42 PM
linux

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... ]
Copy after login

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]#
Copy after login

[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]#
Copy after login
Copy after login

[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]#
Copy after login

[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]#
Copy after login

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]#
Copy after login
Copy after login

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]#
Copy after login

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]#
Copy after login

Execute the command: cat a.txt | sort

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

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]#
Copy after login

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]#
Copy after login

" - "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]#
Copy after login

思考:对于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 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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

See all articles