Command Line Redirection
July 08, 2023 —
Gregg Szumowski
Redirection is very significant in shell scripting. It provides you a
means to save the output of a command to a file or multiple files (one
for stdout
and one for stderr
).
Below is a table of simple redirections that are the most useful in shell scripting. Here we are using the following naming conventions:
stdout
– The output of the script/commandstderr
– The errors generated by the script/commandoutfile
– A target filename where you wish to store the outputerrfile
– A target filename where you wish to store the errors
Command Description/Purpose command 2>errfile Redirect stderr to errfile command >outfile 2>errfile Redirect stderr to file named errfile and stdout to file named outfile command &> outfile Redirect stderr and stdout to outfile command 2>&- Just suppress error messages. No file created. No error message displayed on screen command 2>&1 Redirect error messages to standard output. Useful in shell script when you need to forcefully display error messages on screen
Tags: cli, bash, scripting, redirection, motd