Pipes

You might have heard about or have seen pipes using the | character, they are very frequently used in Linux command-line and can become an essential tool once you master them.

If you have a look at the manual pages related to pipes using man pipe, the definition of a pipe starts with the following sentence :

pipe() creates a pipe, a unidirectional data channel that can be used
       for interprocess communication.

We can infer from it that a pipe :

  • Is a data channel
  • Can be used to communicate data from a process to another

To give you a definition that better fits the concepts we have discussed in the previous class : a pipe is a character whose usage redirects the standard output of a first command part to the standard input of a second one.

For instance, if you wanted to redirect the standard output of whoami to rev, you would type :

$> whoami | rev

New command : rev is like cat, except it reads the data in a reversed way. Try it with rev /etc/passwd or any other file available on hack.courses.

Pipes can be chained without limitations, for instance the following command line is valid :

$> cat /etc/passwd | grep gu3st | rev

Note : The characters “$>” here are not part of the command, they represent the prompt usually displayed by shells to let the user know they can type a new command.

And will cat /etc/passwd, then send all of the file content written to /dev/stdout to grep which fill filter only the lines containing gu3st and then reverse the output with rev.


Redirections

Let’s tackle another important concept in Linux shells : redirections.

They are represented by the characters > and < for the right and left redirection or output and input redirection.

Right redirection (output redirection)

What is meant by right redirection or output redirection is a redirection of a process standard output to a file (which can be a special file or a regular file).

For instance on a Linux system you could write random data to an actual file named random_data using /dev/urandom with the following command :

cat /dev/urandom > random_data

Note : You do not have the permissions to create new files on hack.courses, this command will not work unless you have your own user account.

You could also use right redirections to hide a command’s standard output, like :

ls / > /dev/null

Which would redirect all of the content written by ls / on the standard output to /dev/null.

Hands-on : A good example of this is the ping utility, try with ping 127.0.0.1 > /dev/null. Do you understand which part of the original ping command is written on the standard output? Which one is written on the error output?

Left redirection (input redirection)

Input redirection, using the < operator redirects a file’s contents to a command’s standard input.

You can replace user input, entered using a keyboard with the contents of a special or regular file.

As an example, if we were to sort the contents of a given customers.txt we could use a left redirection like :

sort < customers.txt

The customers.txt will be read by the command interpreter, which will then write all of its contents as sort’s standard input.

Note : There also is another output redirection you might encounter, using two angular brackets >>. This one will add additional data after the end of a file instead of totally replacing its contents like > does.


Hands-On 🤜!

You probably miss actually messing around with a terminal by yourself after all of this! Let’s get our hands dirty and search for more flags by finishing challenges.

If you need a reminder about the flags system, have a look at this section of CLI 101.

Challenge 0

This first challenge’s flag will be given to you by a script, you remember how to run a shell script, right?

Once you run this shell script, it gives you the flag… But wait, there’s something wrong with it isn’t it?

It seems the flag you’re given has been reversed. Maybe you can do something about it using your new command-line skills?

Hint 1

Hint : This exercice can be done (not exclusively) using rev and a pipe.


Whenever you’re ready, move on to :

Command-line 203 (Coming Soon)