Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to execute multiple commands in bash, but have some of them executed in the background. The best way to explain this is using the following command which does not work...

for@barpc:~> (sleep 30;command1;command2 param ) & ; anotherCommand

I guess this could be taken as two questions:

  1. How to terminate the & in a command?
  2. What would be a better way of going about this?

Other details if they matter:

  • Opensuse 13.2
  • gnome-shell
share|improve this question

2 Answers 2

up vote 3 down vote accepted

You should try

(sleep 5; ls) &  echo "Now"

This works as you wish it to.

share|improve this answer
1  
Wow, that is so simple as to have been obvious. Thanks! –  pcnate 20 hours ago
    
Be aware that by doing this you've added an unnecessary sub-shell that will prevent you from waiting for the background job to finish if you ever need to. –  Darkhogg 13 hours ago

All you need to do is remove the ;. & acts as a command separator by itself, so adding ; after that is invalid.

(sleep 30; command1; command2 param ) & anotherCommand

(The spaces after semicolons aren't needed, I just find it easier to read this way. This is one of the few spots where spaces are optional in shell syntax -- most places, they're either required or forbidden.)

As for better ways of doing this... that really depends on exactly what you're trying to do. We'd need more info to make better suggestions.

share|improve this answer
    
Please, please upvote this, it explains why OP's code didn't work instead of blindly throwing brackets hoping it will. –  Darkhogg 13 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.