BASH Programming

Bash Linux Redirection Operators

On Linux, the terminal is everything, it’s where we pass commands, and it’s where we pass scripts. Therefore, one of the most important scripting languages is bash. Bash scripting is used to automate the boring tasks in Linux. In order to automate tasks, commands are written within the script and given an extension of .sh. As part of the commands, certain operators are used as well. These operators are unique to bash, and each has its own specific meaning. In this tutorial, we will review redirection operators encountered during bash scripting and their specific meaning.

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:

#! /bin/bash
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:

#! /bin/bash

cat << EOF
first line
second line

wc << EOF
first line
second line

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:

#! /bin/bash
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:

0 -- Standard input, stdin
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:

#! /bin/bash
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:

#! /bin/bash
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:

#! /bin/bash
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;

bash -cls -la >& file.txt’

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:

bash -c ‘mls -la >& file.txt’

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:

#! /bin/bash

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:

#! /bin/bash

echo “ehlo” >| /tmp/file.txt

Redirection Operator: &>>

The operator &>> will append the standard output and the standard error to the specified file.


#! /bin/bash

lsl  &>> file.txt

In this example, we have a file called file.txt, containing two lines. When we execute the script called, 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:

#! /bin/bash

more <<- EOF
    first line
    second line

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!

About the author

Kalyani Rajalingham

I'm a linux and code lover.