Cancel

Input, Output, and Error Redirection in Bash

Input, Output, and Error redirection is the most important topic to cover in Bash Scripting.

Standard Output, aka stdout, whose symbolic value is > or >>.

1
2
3
4
5
6
7
8
9
# over write data every time you insert.
echo "hello world" > file1.txt

# append data to a file at the end of the line.
echo "hello, Yafiz Abraham" >> file1.txt

# transfer data from one file to another.
cat file1.txt > file3.txt # over write data.
cat file1.txt >> file3.txt # append data.

Standard Error, aka stderr, whose symbolic value is numeric 2> or 2>>.

1
2
3
4
5
6
7
8
# if the cat command failed, then insert the error to a file instead of returning.
cat hello.txt 2> error.log

# if the cat command failed, then append the error to a file at the end of the line instead of returning.
cat hello.txt 2>> error.log

# if the cat command failed, then don't return any error or ignore the error.
cat hello.txt 2>/dev/null

Standard Input, aka stdin, whose symbolic value is < or <<.

1
2
3
# insert dummy data in a file
echo {h..a} | xargs -n 1 > file1.txt
sort < file1.txt # send input to sort command