← Back to Index
ARCHIVED: This article is no longer current

Contents

Start chain of bash commands while typing

Executing the chain cmd1&&cmd2&&…What if the first cmd1 of the chain of bash commands could start executing right away while typing ?

Sometimes, one has multiple bash commands to execute, and knows some of them will take some time. For example, I work in a team, and often need to rebase and rebuild the project locally before pushing the branch again to trigger a nightly CI test round. The easiest trick is to create a bash alias, but in my case, the command vary depending on the exact related task.

Problem: I can type the whole chain of bash commands git pull && git submodule update && configure.py && build.py && run-unit-tests.py && ..., but executing this chain only begins when I press enter in the end. What if the first command git pull could start executing right away while typing ?

Turns out you can: by issuing two distinct commands:

cmd1 &
wait $! && cmd2 && ...

The key takeaway in order to start executing a chain of bash commands while typing, is to issue the first command in background using &, and setup the continuation using wait \(! &&. See documentation about wait and \)!

But what if I already launched the command?

Then slhck has you covered: you can pause the command with <C-Z> and continue typing! fg will resume the first command as soon as you finish typing and press enter.

cmd1
# enter Ctrl-z
fg ; cm2