Timeline for Combine the output of two commands in bash
Current License: CC BY-SA 4.0
24 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Nov 24, 2022 at 11:24 | comment | added | AdminBee | @MikeKulls But if, as OP says neither command exits, then the second command will never run if the first one isn't started in the background. So as far as I can see, a double-ampersand approach wouldn't answer the question in the first place. | |
| Nov 24, 2022 at 11:15 | comment | added | MikeKulls | @AdminBee I think offering a solution that runs in the background as the default option is a bad answer. I would expect the first command to run, in the foreground followed by the second command in the foreground. If the person answering had offered background processing as an alternate solution then fair enough. Or if they'd explained foreground vs background better it might make sense. End of the day double ampersand is far better and is going to give the behaviour that 99.9% of people expect. This is what the voting system is for to put the best answers forward. This isn't the best answer. | |
| Nov 22, 2022 at 13:00 | history | edited | Gilles Quénot | CC BY-SA 4.0 | added 99 characters in body |
| Nov 22, 2022 at 12:56 | comment | added | AdminBee | @MikeKulls please read the reply by Zoey to the comment of DUzun who made the same remark. | |
| Nov 22, 2022 at 12:45 | history | edited | Gilles Quénot | CC BY-SA 4.0 | added 119 characters in body |
| Nov 22, 2022 at 1:06 | comment | added | MikeKulls | I've given this a down vote due to use of & instead of &&. Using & makes the command not very general purpose. For example, if I run a long running command on the console I can't stop it because it's executing in the background | |
| Nov 6, 2020 at 16:30 | comment | added | alper | Can we write alias for { command1 & command2; } ? | |
| May 17, 2020 at 4:28 | history | edited | Gilles Quénot | CC BY-SA 4.0 | added 4 characters in body |
| Jan 14, 2019 at 18:59 | comment | added | danielpops | also note that the space between the open and closing bracket is required! | |
| Jun 2, 2018 at 6:32 | comment | added | DUzun | @ZoeyHewll I see what you mean. { yes; echo no; } in this case will output 'no' after you press Ctrl+C. { yes & no; } will scrumble output of yes with 'no', and most likely 'no' would be on its own line. The right tool for the right task. | |
| Jun 1, 2018 at 5:42 | comment | added | Zoey Hewll | @DUzun "Neither command exits so I'm not sure how to do this." The OP specified that the commands produce indefinite streams of output, and do not exit. This is not comparable to commands which produce finite output and then end. For example, { yes && echo no } will never print "no" | |
| Jun 1, 2018 at 5:19 | comment | added | DUzun | @ZoeyHewll command1 and command2 are just placeholders - replace them with real commands that produce some output. As an example: { echo 123 && echo 321; } > /tmp/test | |
| May 31, 2018 at 2:25 | comment | added | Zoey Hewll | @DUzun OP said neither command exits, so with your solution, the second command will never run | |
| Apr 5, 2018 at 11:04 | comment | added | DUzun | I would rather use && instead of &! command1 & command2 - this runs command1 in background and starts command2 immediately, thus running both commands in parallel and messing up the output. command1 && command2 - this runs command1 (in foreground) and then, if command1 succedded, runs command2. | |
| S Dec 9, 2017 at 13:29 | history | suggested | Дмитро | CC BY-SA 3.0 | add required info from comment |
| Dec 9, 2017 at 7:47 | review | Suggested edits | |||
| S Dec 9, 2017 at 13:29 | |||||
| Jan 17, 2017 at 1:41 | comment | added | antak | Be warned: This doesn't preserve whole lines! You'll get unreliable outputs as lines get split up part way and mixed up among each other. You can try this with { yes {1..20} & yes {1..20}; } | grep -v '^1 2 3' which ideally won't print anything if lines aren't broken. | |
| Mar 27, 2016 at 11:26 | comment | added | Mike D | works great. tested by starting multiple static-server one-liners piped to cat and grep --line-buffered. also, SuperMagic's statement was true for me, ^c only killed one of the commands, rest stayed bg'd. | |
| Dec 2, 2014 at 18:27 | comment | added | Gilles Quénot | Seems like you lack the last ; before }, it's mandatory ! | |
| Jul 31, 2014 at 16:39 | comment | added | Petr Peller | Does not work for me either. I am simply combining two grep commands but when grouped no output is produced and program seems waiting for input. | |
| Feb 14, 2013 at 4:58 | comment | added | SuperMagic | Doesn't matter that they programs don't finish. 'tail -f' doesn't "finish" either, but this still works and combines the outputs of both programs. Works for more than two commands as well. ^c to quit kills only one of the grouped commands. You'll have to kill the other's manually, though. | |
| Feb 14, 2013 at 0:49 | history | edited | Gilles Quénot | CC BY-SA 3.0 | added 124 characters in body |
| Feb 14, 2013 at 0:41 | history | edited | jordanm | CC BY-SA 3.0 | Change the command terminator to better align with the OP's scenerio |
| Feb 13, 2013 at 23:28 | history | answered | Gilles Quénot | CC BY-SA 3.0 |