14

Am I correct to assume that when ; joins two commands on a line, Bash always waits until the first command has exited before executing the second command? And similarly, in a shell script containing two different commands on different lines, Bash always waits until the command on the first line has exited before executing the command on the second line?

If this is the case, is there a way to execute two commands in one line or in a script, so that the second command doesn't wait until the first command has finished?

Also, are different lines in a shell script equivalent to separate lines joined by ; or &&?

26

You're correct, commands in scripts are executed sequentially by default.

You can run a command in the background by suffixing it with & (a single ampersand).

Commands on separate lines are equivalent to commands joined with ; by default. If you tell your shell to abort on non-zero exit codes (set -e), then the script will execute as though all the commands were joined with &&.

| improve this answer | |
2

To answer your second question, you can use & to launch a command in the background and continue with the script without waiting for it to finish.

commandA & commandB

If you run this at an interactive terminal (instead of a script), you can use fg to bring the backgrounded command back into focus, or jobs to see a list of background tasks.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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