[{"Type":"MASTER","Line of Business":{"code":"LOB57","label":"Power"},"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Product":{"code":"SWG60","label":"IBM i"},"Platform":[{"code":"PF012","label":"IBM i"}],"Version":"6.1.0"}]
First thing to note is that there's couple of ways depending on your purpose and shell, therefore this requires slight understanding of multiple aspects. Additionally, certain commands such as time and strace write output to stderr by default, and may or may not provide a method of redirection specific to that command
Basic theory behind redirection is that a process spawned by shell (assuming it is an external command and not shell built-in) is created via fork() and command [arg1] [arg2] 2> /dev/null 0 syscalls, and before that happens another syscall command [arg1] [arg2] 2> /dev/null 1 performs necessary redirects before command [arg1] [arg2] 2> /dev/null 0 happens. In that sense, redirections are inherited from the parent shell. The command [arg1] [arg2] 2> /dev/null 3 and command [arg1] [arg2] 2> /dev/null 4 inform the shell on how to perform command [arg1] [arg2] 2> /dev/null 5 and command [arg1] [arg2] 2> /dev/null 1 syscall (see also How input redirection works, What is the difference between redirection and pipe, and What does & exactly mean in output redirection )
Shell redirections
Most typical, is via command [arg1] [arg2] 2> /dev/null 7 in Bourne-like shells, such as command [arg1] [arg2] 2> /dev/null 8 (which is symlinked to command [arg1] [arg2] 2> /dev/null 9) and #!/bin/sh exec 2> ./my_log_file.txt stat /etc/non_existing_file 0; first is the default and POSIX-compliant shell and the other is what most users use for interactive session. They differ in syntax and features, but luckily for us error stream redirection works the same (except the #!/bin/sh exec 2> ./my_log_file.txt stat /etc/non_existing_file 1 non standard one). In case of csh and its derivatives, the stderr redirection doesn't quite work there.
Let's come back to command [arg1] [arg2] 2> /dev/null 7 part. Two key things to notice: #!/bin/sh exec 2> ./my_log_file.txt stat /etc/non_existing_file 3 means redirection operator, where we open a file and #!/bin/sh exec 2> ./my_log_file.txt stat /etc/non_existing_file 4 integer stands for stderr file descriptor; in fact this is exactly how POSIX standard for shell language defines redirection in section 2.7:
[n]redir-op wordFor simple #!/bin/sh exec 2> ./my_log_file.txt stat /etc/non_existing_file 3 redirection, the #!/bin/sh exec 2> ./my_log_file.txt stat /etc/non_existing_file 6 integer is implied for #!/bin/sh exec 2> ./my_log_file.txt stat /etc/non_existing_file 7, i.e. #!/bin/sh exec 2> ./my_log_file.txt stat /etc/non_existing_file 8 is just the same as #!/bin/sh exec 2> ./my_log_file.txt stat /etc/non_existing_file 9. Note, that the integer or redirection operator cannot be quoted, otherwise shell doesn't recognize them as such, and instead treats as literal string of text. As for spacing, it's important that integer is right next to redirection operator, but file can either be next to redirection operator or not, i.e. some_function(){ command1 command2 } 2> my_log_file.txt 0 and some_function(){ command1 command2 } 2> my_log_file.txt 1 will work just fine.
The somewhat simplified syntax for typical command in shell would be
command [arg1] [arg2] 2> /dev/nullThe trick here is that redirection can appear anywhere. That is both some_function(){ command1 command2 } 2> my_log_file.txt 2 and some_function(){ command1 command2 } 2> my_log_file.txt 3 are valid. Note that for #!/bin/sh exec 2> ./my_log_file.txt stat /etc/non_existing_file 0 shell, there there exists #!/bin/sh exec 2> ./my_log_file.txt stat /etc/non_existing_file 1 way to redirect both stdout and stderr streams at the same time, but again - it's bash specific and if you're striving for portability of scripts, it may not work. See also Ubuntu Wiki and .
Note: The #!/bin/sh exec 2> ./my_log_file.txt stat /etc/non_existing_file 3 redirection operator truncates a file and overwrites it, if the file exists. The some_function(){ command1 command2 } 2> my_log_file.txt 7 may be used for appending some_function(){ command1 command2 } 2> my_log_file.txt 8 to file.
If you may notice, #!/bin/sh exec 2> ./my_log_file.txt stat /etc/non_existing_file 3 is meant for one single command. For scripts, we can redirect stderr stream of the whole script from outside as in time echo foo 2>&1 > file.txt 0 or we can make use of exec built-in. The exec built-in has the power to rewire the stream for the whole shell session, so to speak, whether interactively or via script. Something like
#!/bin/sh exec 2> ./my_log_file.txt stat /etc/non_existing_fileIn this example, the log file should show time echo foo 2>&1 > file.txt 1.
Yet another way is via functions. As kopciuszek noted in his answer, we can write function declaration with already attached redirection, that is
Commands writing to stderr exclusively
Commands such as time and strace write their output to stderr by default. In case of time command, the only viable alternative is to redirect output of whole command , that is
time echo foo 2>&1 > file.txtalternatively, synchronous list or subshell could be redirected if you want to separate the output ( as shown in related post ):
{ time sleep 1 2> sleep.stderr ; } 2> time.txtOther commands, such as strace or time echo foo 2>&1 > file.txt 6 provide means to redirect stderr. strace has time echo foo 2>&1 > file.txt 8 option which allows specifying filename where output should be written. There is also an option for writing a textfile for each subprocess that strace sees. The time echo foo 2>&1 > file.txt 6 command writes the text user interface to stdout but output to stderr, so in order to save its output to variable ( because { time sleep 1 2> sleep.stderr ; } 2> time.txt 1 and pipelines only receives stderr ) we need to swap the file descriptors
result=$(dialog --inputbox test 0 0 2>&1 1>/dev/tty);but additionally, there is { time sleep 1 2> sleep.stderr ; } 2> time.txt 2 flag, which we also can utilize. There's also the method of named pipes. I recommend reading the linked post about the time echo foo 2>&1 > file.txt 6 command for thorough description of what's happening.