Command Input and Output (I/O) in Ubuntu / Linux

1. Output Stream - Std Output & Std Error

Hello there, although each of the Linux commands are very powerful on their own, the true power of the Linux come online 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 change 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 article, you’re going to learn about the different ways that a command can take input and give output using pipelines.

$ INPUT (STD INPUT + COMMAND ARGUMENT)   →  COMMAND   →  OUTPUT (STD OUTPUT + STD ERROR )

So you’ve got standard input, command arguments, standard output and standard error,  they’re all standard data streams, whereas the command line arguments aren’t a standard data stream. 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, for example, let’s take the data command.If we type data and press enter when we tell the data command to run, the output will be sent from the data command and flow down the standard output stream, which by default is connected to our terminal screen.

$ date

Well, with the date command the result is streamed to the output, which is by default is connected to terminal screen.

Hence, Amazing thing about output data streams is that you can redirect where they go using a process, imaginative, imaginatively called redirection. So just like redirecting a pipe will change where water flows, redirecting a data stream will change where the data goes.

So now when a command gives an error message or a log message or something, 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 date command again.If I make the date command fail by giving it some invalid command argument like this ‘ sgdfdlfjdljffd’
, and then I press enter, you’ll see that we get an error message and it says invalid date ‘ sgdfdlfjdljffd’

OK, 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.Hence the error message is output is terminal also.

$ date sgdfdlfjdljffd

Most Importantly, we can redirect the output stream to a particular file or any other standard output like this 

$ cal -A 1 -B 1 12 2022 >> a.txt

Thus with the above example, we can see that the standard output redirected in the file a.txt using >> operator is same as showing on the terminal without redirecting to the file, but redirecting to the standard output that is terminal  

2. Input Stream - Std Input & Command Argument

Well, now we will discuss is another type of standard data stream, which, as you can guess, is called standard input. Standard input is by default connected to the keyboard. So to demonstrate this, I will introduce a new command called cat.

$ cat
 

So let’s do that if I just type cat and don’t give it any other form of input, we can see the terminal kind of just hangs there.But if I start typing and I type “Hello World” and press enter, we see that the same text is read from our keyboard, i.e. from standard input and then cat processes it and outputs the results to standard output that is by default attached to our terminal, we see the output appear right in our terminal.

Now going ahead and we can say that the command arguments have their own inputs , as showing here 

$ cal -A 1 -B 1 12 2017

So it was quite complicated and it showed us the calendar for December twenty seventeen with one month after and one month before.Now the A and B options have their own command line arguments hereThe option has the command line argument, which is number one, and B also has a command line argument of the number one and the output is streamed to the default output screen that is terminal.

You can take input from the file , Standard Input can be changed from keyboard to the file. Please look in to the following example

$ cat < a.txt
 

That’s all for the Input / Output data streams in Ubuntu Linux

Leave a Reply