Aickson
Aickson

Reputation: 151

Run a command in background and another command in frontground in the same line

I want to run 2 commands (command1 and command2) in the same line, where command1 starts a background process and command2 starts a frontground process.

I tried:

command1 & ; command2

But it says: "-bash: syntax error near unexpected token `;'"

How could I run the 2 commands in the same line?

Upvotes: 0

Views: 167

Answers (2)

fedorqui
fedorqui

Reputation: 289505

; is not helping here. The control operator you need here is & after the first command (thanks Nick Russo in comments):

command1 & command2

From man bash:

If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0.

Commands separated by a ; are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed.

Test

$ sleep 10 & echo "yes"
[2] 13368
yes
$ 
[1]-  Done                    sleep 10
$ 

Upvotes: 2

Stefano Falsetto
Stefano Falsetto

Reputation: 572

Try this:

(command1 &); command2

The the syntax (command) is creating a "subshell". You can read here something about it.

Upvotes: 2

Related Questions