HOME ABOUT CONTACT ARTICLES

Special Chars: Redirection (>, >>)


Redirection allows us to redirect the output (stdout) of one command to another command

Examples

Supress command output

Commands can be suppressed by redirecting the output to the null file /dev/null
In the belong example usually we'd expect something printed to the terminal, but since
we are redirecting the output it will not do so.

luke@nebulae:~$ echo "Testing no output" > /dev/null
luke@nebulae:~$


Redirect output (stdout) to file

This will overwrite the file test.txt and set its contents to "Hello World"

echo "Hello world!" > test.txt


Redirect and append output (stdout) to file

This will append to the file test.txt and append "Hello World" to the last line

echo "Hello world!" >> test.txt

As an example:

luke@nebulae:~$ echo "Will be line 1" >> test.txt
luke@nebulae:~$ echo "Will be line 2" >> test.txt
luke@nebulae:~$ echo "Will be line 3" >> test.txt
luke@nebulae:~$ cat test.txt
 Will be line 1
 Will be line 2
 Will be line 3


Redirect error (stderr) to file

This will overwrite the file test.txt and set its contents to the outputted error

echo "Hello world!" 2> error_log.txt

As an example:

luke@nebulae:~$ echoError "Hello world!" >> error_log.txt
luke@nebulae:~$ cat error_log.txt
 bash: echoError: command not found