Hello there, you beautiful people.
Now, although each of the Linux commands are very powerful on their own, the true power of the Linux
command line comes from our ability to chain these commands together to build what are known as command
pipelines.
Now, each Linux command is designed to do one thing and do it incredibly well.
So by being able to combine together loads of powerful and feature rich building blocks, you can create
very powerful workflows and pipelines without much work at all.
But in order to chain the commands together and create these powerful workflows, these pipelines,
we need to understand a bit better how Linux commands actually take input and give output so that we
can link the outputs and inputs together.
So in this video, you're going to learn about the different ways that a command can take input and
give output, and by the end you'll be able to understand the different ways that data can flow into
and out of a command.
And you'll be ready to connect these commands, these data flows together to build powerful Linux pipelines.
This is going to be an absolutely revolutionary time for your Linux abilities because we're going to
step up from taking just one command and learning about it to actually learn how to chain multiple commands
together.
So I hope you're very, very excited.
Let's go ahead and jump right into it.
So as you learned earlier, each command is basically its own computer program.
Now, a computer program is simply an automated way to take inputs, process them and give outputs.
So here's a diagram that shows you the different ways that a command can take inputs and give outputs.
Now you can see that a general Linux command can take input from two places and will output to two other
places, making a total of four possible ways to get data into and out of a conventional Linux command.
So the inputs are command arguments and standard input, and the outputs are standard output and standard
error.
You can see that three of these are indicated by the yellow dashed arrows and are examples of something
called a standard data stream.
You can tell this from their names because they all start with the word standard.
So you've got standard input, standard output and standard error, and they're all standard data streams,
whereas the command line arguments have a solid blue arrow and aren't a standard data stream.
So in this video, I'm going to explain to you what the different input or output options are, and
you'll also learn what is special about a standard data stream and how these data streams can be used
to connect commands together.
So let's jump over to Ubuntu and see all this stuff in real time.
Okay.
So for this discussion, it makes more sense to start with the outputs.
So let's start there.
Now, when a command has run successfully, the primary way that the data is outputted from the command
is on the channel called Standard Output.
Now, standard output is something called a standard data stream.
Now, just like a stream of water, data, streams start somewhere and they end somewhere.
So where does standard output lead?
Well, by default, standard output will lead to your terminal.
So that's why when we type commands, the output of the command appears on the screen.
So for example, let's take the data command.
If we type, date and press enter when we tell the date command to run, the output will be sent from
the date command and flow down the standard output stream, which by default is connected to our terminal
screen.
Therefore, we see the data appear below our command.
Okay, so that's pretty cool.
But I want to plant a little seed in your mind for us to come back to later.
Now, remember how I said that by default, the standard output of a command comes to the terminal?
Well, the amazing thing about output data streams is that you can redirect where they go using a process
imaginatively, imaginatively called redirection.
So just like redirecting a pipe will change where water flows, redirecting a data stream will change
where the data goes.
Okay, so hopefully that seed has been planted and we'll come back to it in a moment.
Now, the second type of output is something called standard error.
So when a command gives an error message or a log message or something like that, anything that isn't
the primary output of the command that is outputted into the stream known as standard error, this helps
keep useful output and log or error information separate.
So for example, let's take the data command again.
If I make the date command fail by giving it some invalid command argument like this, okay.
And then I press enter, you'll see that we get an error message and it says Invalid date blue a little
lower now because it's an error message.
It was sent down the standard error stream and because the error message arrived at our terminal screen,
we can see that the standard error is also automatically connected to our terminal, just like standard
output is.
But again, because standard error is a data stream, it can be redirected wherever we wish.
So now that we understand that standard output is where the main output for the commands go, and that
standard error is where the error and log messages for the command go and that both of those are by
default connected to the terminal.
Let's now turn our attention to inputs.
As mentioned, there are also two ways for a command to get input.
The first we will discuss is another type of standard data stream, which, as you can guess, is called
standard input.
Now standard input is by default connected to the keyboard.
So to demonstrate this, I will introduce a new command called Cat.
Now we're going to discuss Cat in a later section, but I'll just use it now to explain the concept
of standard input.
If we take a look at the main page for Cat, so we type man cat, we see in its description section
the if cat is not given any.
If it's not given any, how can I say it's not giving any command line arguments?
That's it.
Cat will read from standard input.
So if no file is given, then read it or it'll read from standard input.
So let's do that.
If I just type cat and don't give it any other form of input, we can see that the terminal kind of
just hangs there.
But if I start typing and I type hello and press enter, we see that the text is read from our keyboard,
i.e. from standard input and then cat processes it and outputs the results to standard output.
Now, because standard output is by default attached to our terminal, we see the output appear right
in our terminal.
Okay, that's pretty awesome.
Now because standard input is a data stream, it can be redirected as well.
That means we can tell a command where it should read data from.
Now this could be a file where we already have some pre configured input or more powerfully it could
be another data stream.
Now is this ability for data streams to connect together that makes them so powerful, you can simply
pass the standard output stream from one command to the standard input stream of another, then pass
the standard output stream of that second command to the standard input stream of the third command
and so on and so on until you build up a very powerful pipeline.
Now, connecting outputs to inputs in this way is known as piping together commands and is an incredibly
important concept in Linux as it's what makes working with the command line so powerful and effective,
as you will see in coming videos.
Now the final type of input is command line arguments.
Now, we've already actually seen command line arguments in action already.
For example, when studying the command structure previously we took a look at the core command and
we saw, by the way, I'm going to press control and see to cancel this control and see and that cancels
it and press control now to clear the screen.
So when we're using the command, we saw something like Cal 12, 2017, which will allow us to see the
calendar for December 2017.
And the numbers 12 and 2017 were command line arguments.
And you can tell that because they were added on the command line as an input to the command, we also
made the command more advanced like this.
If you remember, we had Cal gave it the capital ay option, gave that option and input gave the capital
B option, gave that option its own input and then gave the Cal command to other inputs.
So it was quite complicated and it showed us the calendar for December 2017 with one month after and
one month before.
Now the A and B options have their own command line arguments.
Here, the aye option has the command line argument, which is number one, and B also has a command
line argument of the number one.
And on top of those, the overall Cal Command has 12 and 2017 as its own command line arguments.
So there's one argument for the option, one argument for the B option and two arguments for the overall
CAL command.
Now, the difference between command line arguments and data streams is that data streams can flow,
they can be redirected and piped together.
Command line arguments only associate with the command or option that they're dealing with at the moment.
Now there are commands such as the X ARGs Command that allow to blur the lines a little bit between
data streams and command line arguments.
But in general, the difference is that data streams can flow and be piped together, but command line
arguments are more static.
And still, there is one other key difference to keep in mind, though, in that not all commands accept
standard input.
For example, the Echo Command doesn't accept standard input, but pretty much every command can accept
command line arguments.
And if you want to know whether a command can actually accept standard input, just check its main page.
And if it's not there, just assume that it can't.
Awesome.
So in this video you've learned some incredibly important concepts.
As a quick recap, we learnt that there are two ways to get data into commands and two ways to get data
out.
The ways in our standard input and command line arguments and the ways out are standard output and standard
error.
Now, the three ways that start with the word standard are examples of standard data streams.
We mentioned that standard output is where the outputted data for a command goes, and that standard
error is where the error and log messages for a command goes.
And by default, standard output and standard error are attached to your terminal.
But this can be changed.
You also learned about standard input.
Now standard input by default is attached to your keyboard and is also a data stream, which means it
can take data from other places too, using something called redirection.
That's right.
Data streams can be redirected from their default locations to wherever you wish, using a process called
redirection.
We also mentioned that you can actually connect the output streams of one command to the standard input
of another using a method called piping.
Now, piping and redirection are incredibly powerful abilities that Linux gives us, and being able
to use them effectively will basically give you super powers for interacting with your computer in ways
that you never thought possible.
And I hope that you're super excited because piping and redirection are going to be the topic of the
next few lectures.
So if you want to gain super powers, I'll see you in the next video.