Redirection of results and input is an all-natural function of any development or scripting language. Technically, it happens whenever you connect to a computer inherently. Input gets to go through from stdin (standard input, generally your keyboard or mouse), the output would go to stdout (standard output, a text message or data stream), and errors get delivered to stderr. Knowing that these data streams can be found enables you to regulate where information goes if you are using a shell, such as for example Zsh or Bash.
You should get your feet wet by using these tutorials and getting to know linux better on a Cloud VPS from here.
Standard in, standard out, and standard error exist as filesystem locations on Linux. You can see them in /dev
:
$ ls /dev/std*
/dev/stderr@ /dev/stdin@ /dev/stdout@
You can’t carry out much with them directly, but it’s sometimes beneficial to think about them as meta-locations where one can send data.
The fundamentals of redirection are simple: use some number of > characters to redirect output, plus some true number of < characters to redirect input.
Redirecting output
To write the output of the ls command to a file:
$ ls > list.txt
You don’t see the output of ls as you normally would because the output is going to be written to the list.txt file rather than your screen. This is therefore versatile, in fact, that you can also use it to copy the contents of one file to another. It doesn’t have to be a text file, as well. You can use redirection for binary data:
$ cat image.png > picture.png
Redirecting input
It is possible to redirect input “into” a command, too. It could be useful, however, when a list is expected by a command of arguments, and you also have those arguments in a document and want to quickly “copy and paste” them from the document into the terminal (except you do not really need to copy and paste): That is probably less useful than redirecting result because many commands already are hard-coded to consider input from arguments you provide.
$ sudo dnf install $(<package.list>)
Common uses of input redirection are the here-document (or just here-doc for short) and here-string techniques. This input method redirects a block of text into the standard input stream, up to a special end-of-file marker (most people use EOF, but it can be any string you expect to be unique). Try typing this (up to the second instance of EOF) into a terminal:
$ echo << EOF
> foo
> bar
> baz
> EOF
The expected result:
foo
bar
baz
A here-doc is a common technique utilized by Bash scripters to dump several lines of text into a document or onto the display. So long as you do not forget to end the clause together with your end-of-file marker, it’s a highly effective means of avoiding unwieldy lists of echo or printf statements.
A here-string is comparable to a here-doc, nonetheless it consists of just one single string (or many strings disguised as an individual string with quotation marks):
$ cat <<< “foo bar baz”
foo bar baz
Redirecting error messages
Redirecting error messages
Error messages go to a stream called stderr
, designated as 2>
for the purposes of redirection. This command directs error messages to a file called output.log
:
$ ls /nope 2> output.log
0 Comments