redirection advanced with pipe and file io
################################
comm short for command/program name
a good program to test all of this with is:
service --status-all , this has different output for error and for stdout
commands that start with * is going to work with bash 4 and newer
commands that start with # should work with bash 3 and older
There are 2 ways redirections will save to a file, either overwrite, where it will make a new file if needed and if file exists it will remove its contents first then write the new stuff in it (all previous contents will be gone), the other mode is append mode, new stuff goes to the end of the file.
That doesnt apply to pipes
PIPES
#######
FILE IO
########
# comm1 >> file 2>> file.err comm1 output will not be shown at all on the screen as all forms of output (stdout and stderr) are redirected to files, stdout will go to file (append mode), and stderr will go to file.err (append mode). # comm1 >> file 2> file.err comm1 output will not be shown at all on the screen as all forms of output (stdout and stderr) are redirected to files, stdout will go to file (append mode), and stderr will go to file.err (overwrite mode).
NOTE: cant write to the same file like this, behavior is weird # comm1 > file 2>> file.err comm1 output will not be shown at all on the screen as all forms of output (stdout and stderr) are redirected to files, stdout will go to file (overwrite mode), and stderr will go to file.err (append mode).
NOTE: cant write to the same file like this, behavior is weird
1 2 3 |
# comm1 > file 2> file # comm1 > file 2>&1 * comm1 &> file |
NOTE: all 3 commands will work, last command will work in bash 4
1 2 3 |
# comm1 >> file 2>> file # comm1 >> file 2>&1 * comm1 &>> file |
comm1 output will not be shown at all on the screen as all forms of output (stdout and stderr) are redirected to the same file. stdout and stderr will be written to file in (append mode).
NOTE: all 3 commands will work, last command will work in bash 4
NOTE: no such thing as “# comm1 >> file 2>>&1” instead its just “# comm1 >> file 2>&1”, why because if it was “>> and 2>>&1” then that would mean that “>> and 2>&1” exists, but that behaviour is unstable