[ad_1]
Even though I could spend the rest of my days with Linux and never touch the command line, I still opt to work with the terminal because it’s not only efficient, but it has a lot of really handy tricks I can use. One such trick is called “piping.”
Effectively, piping is taking the output of the first command and using it in the next command. And you can do this with as much piping as you like. You can pipe the output of Command A to Command B, and then pipe the output of Command B to Command C, and then pipe the output of Command C to Command D…etc.
Also: 8 things you can do with Linux that you can’t do with MacOS or Windows
Piping sends data from one command to be used by the next in sequence — and it continues doing so until you’ve run the final command, with the data always flowing from right to left. That’s an important piece of information, and the flow of data piping is always in one way.
How to use piping in Linux
What you’ll need: Piping in the Linux command line works on every Linux distribution, which means all you need is a running instance of any Linux distribution.
Also: How to choose the right Linux desktop distribution for you
The syntax of a piped command looks like this:
Command 1 | Command 2 | Command 3
The | character indicates the piping, and bash reads it as such.
Let’s first talk about the commands we’ll pipe together.
Before we actually get to the piping, I’ll first demonstrate the creation of a new file that contains a list of colors. Create the file with the command:
In that file, paste the following:
Orange
Yellow
Red
Blue
Green
Purple
Black
Pink
Save and close the file.
The output should look like this:
Black Blue Green Orange Pink Purple Red Yellow
Instead of running these commands separately, we can use a combination of && and | to do this on one line. I’ve already discussed how to combine Linux commands for a more efficient experience using the && characters. We’ll make use of that technique with our piping command.
First, we’re going to create our colors.txt file and add the content with two commands that will be joined together with &&, like so:
touch colors.txt && echo -e "OrangenYellownRednBluenGreennPurplenBlacknPink" >> colors
What you see above uses the -e option to inform the echo command to interpret escape sequences. In this case, the n escape sequence creates a new line after each color.
Also: Want to save your aging computer? Try these 5 Linux distributions
Before that, we use the touch command to create the new file. So, the file is created and then content is added, one line at a time.
Before we do anything, make sure to delete the current colors.txt file with the command rm colors.txt.
Also: The best laptops for every type of person and budget
What we’ll do now is add our first two commands and then use the cat and sort commands via piping. Cat reads content of a file to the terminal, and sort will sort the output. All together, this command will look like this:
touch colors.txt && echo -e "OrangenYellownRednBluenGreennPurplenBlacknPink" >> colors.txt && cat colors.txt | sort
The output of the above command should look like this:
Black Blue Green Orange Pink Purple Red Yellow
One thing to keep in mind here is the only piping done in the above command is cat colors.txt | sort. The output of the cat colors.txt command is sent to the sort command, which displays the results.
Also: How to enable Linux on your Chromebook (and why you should)
And that’s how piping works with the Linux command line. This is a very handy trick you can use to make working with the command line even more efficient.
[ad_2]
Source link