filmov
tv
Unix & Linux: Combine the output of two commands in bash (8 Solutions!!)

Показать описание
Unix & Linux: Combine the output of two commands in bash
The Question: Is it possible to combine output from these two commands?
Neither command exits so I'm not sure how to do this.
Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful
== This solution helped 129 people ==
You can combine two commands by grouping it with { } :
{ command1 & command2; }
so far, you can redirect the group to a file (last ; before } is mandatory),
and the space between the open and closing bracket too.
{ command1 & command2; } > new_file
if you want to separate STDOUT and STDERRin two files :
{ command1 & command2; } > STDOUT_file 2> STDERR_file
== This solution helped 2 people ==
Try this:
outputfile
== This solution helped 2 people ==
I ended up doing this, the other suggestions did not work, as the 2nd command
was either killed or never executed.
alias app () {
tail -f /tmp/log
}
== This solution helped 63 people ==
More generally, it's possible to use either a subshell or command grouping, and
redirect the output of the whole group at once.
Code:
( command1 ; command2 ; command3 ) | cat
The main difference between the two is that the first one splits of a child
process, while the second one operates in the context of the main shell. This
can have consequences regarding the setting and use of variables and other
environment settings, as well as performance.
Don't forget that the closing bracket in command grouping (and functions) must
be separated from the contents by either a semicolon or a newline. This is
because "}" is actually a command (keyword) of its own, and must be treated
like one.
The Question: Is it possible to combine output from these two commands?
Neither command exits so I'm not sure how to do this.
Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful
== This solution helped 129 people ==
You can combine two commands by grouping it with { } :
{ command1 & command2; }
so far, you can redirect the group to a file (last ; before } is mandatory),
and the space between the open and closing bracket too.
{ command1 & command2; } > new_file
if you want to separate STDOUT and STDERRin two files :
{ command1 & command2; } > STDOUT_file 2> STDERR_file
== This solution helped 2 people ==
Try this:
outputfile
== This solution helped 2 people ==
I ended up doing this, the other suggestions did not work, as the 2nd command
was either killed or never executed.
alias app () {
tail -f /tmp/log
}
== This solution helped 63 people ==
More generally, it's possible to use either a subshell or command grouping, and
redirect the output of the whole group at once.
Code:
( command1 ; command2 ; command3 ) | cat
The main difference between the two is that the first one splits of a child
process, while the second one operates in the context of the main shell. This
can have consequences regarding the setting and use of variables and other
environment settings, as well as performance.
Don't forget that the closing bracket in command grouping (and functions) must
be separated from the contents by either a semicolon or a newline. This is
because "}" is actually a command (keyword) of its own, and must be treated
like one.