Redirection Operator: <
The symbol < is used for input redirection. Files, for example, can be used as input. However, in this case, the input redirection is a read-only redirection.
For example:
cat < file.txt
In this case, the file.txt is taken as the input, and the cat command then cats it out.
Redirection Operator: <<
The redirection operator << is also known as the here-document. The here-document allows one to put a line of input into a command in many.
For example:
cat << EOF
first line
second line
EOF
wc << EOF
first line
second line
EOF
Ok, so here, we have two lines of input. In the first section, we send the input to the cat, which cat it all out. And in the second section, we count the number of lines, words, and characters using the wc command. Either way, the point is that we could send multiple lines in as input instead of a single line.
Redirection Operator: >
This symbol, known as the file redirection operator, is typically used to redirect the contents of a command/file to another by overwriting it. Mind you; it overwrites it – in bold and italicized!
For example:
echo “hello world’ > file.txt
Here, the > symbol is similar to 1>. This is so because the 1 is a file descriptor for the standard output. Please note that the file descriptors are as follows:
1 -- Standard output, stdout
2 -- Standard error, stderr
In the previous scenario, the single forward arrow was equivalent to 1>. However, we can also write 2> to forward the standard error.
For example:
mcat file.txt 2> file2.txt
Here, the 2> means that the error generated will be dumped into file2.txt.
Redirection Operator: >>
The symbol >> is used to append and not to replace! The file redirection operator > replaces or overwrites everything while the >> is used to append. The latter will add the contents specified to the end of the file.
For example:
echo “this is the second line” >> file.txt
echo “this is the third line” >> file.txt
The latter will append the two lines to the file called file.txt. The result of file.txt will then be as follows:
Redirection Operator: |
The redirection operator | is used to send the output of the first command as the input of the second command. For example, if I pass an initial command and then “pipe” the output generated by this command by using the | operator into a second command, it will be received as the input and then processed.
For example:
ls-la | sed ‘s/bash/redirection_operator/’
Here, sed with the s command is used to substitute one name for another. So, sed ‘s/bash/redirection_operator/’ is used to replace the word bash with the word redirection_operator.
So what are we doing here? Well, ‘ls -la’ will list everything in detail, and the pipe operator will take this and send it to the second command. The second command (sed ‘s/bash/redirection_operator/’) will substitute the word bash with the word redirection_operator, and print it to the screen.
Redirection Operator: >&
This symbol redirects both the standard output and the standard error.
For example;
In this case, the >& symbol redirects both the standard output and the standard error to the file called file.txt. Thus, both the output generated and the error generated is placed in the same file.
Now suppose we write this instead:
In this case, an error should be generated because there is no mls command. Here, the error will also be sent to the file.txt document.
Redirection Operator: >|
There are times when you cannot overwrite a file because of file restrictions. Now suppose that you have a file called file.txt which cannot be overwritten.
So the following command will not actually overwrite a file:
echo “ehlo” > /tmp/file.txt
We use the operator >| to forcibly overwrite the file in such cases.
Here, we’d write the following to overwrite the file forcibly:
echo “ehlo” >| /tmp/file.txt
Redirection Operator: &>>
The operator &>> will append the standard output and the standard error to the specified file.
Ex:
lsl &>> file.txt
In this example, we have a file called file.txt, containing two lines. When we execute the script called bash.sh, which contains a command that doesn’t exist, this should throw an error. This error is caught and appended onto file.txt. If the command had not contained any errors, it would have caught it as well and sent it to append it to the file called file.txt.
Redirection Operator: <<-
The redirection operator <<- will strip all tabs from a multi-line input.
For example:
more <<- EOF
first line
second line
EOF
Here there are tabs before the two lines of input (first line, second line). But when the output is generated, the tabs are ignored.
Redirection Operator: <>
The redirection operator <> opens a file for both reading and writing.
Bash scripting is a key scripting language that can be used to automate tasks. During bash scripting, we encounter much code, but we also encounter redirection operators that are unique to bash. These operators each have a particular role in bash scripting, and they aren’t always obvious. In this tutorial, we reviewed a few redirection operators used while writing bash scripts. Obviously, there are many redirection operators out there; however, some are encountered so frequently that it might be necessary to know them while bash scripting. So go forth, fearless of redirection operators from here onwards!
Happy Coding!