Difference between revisions of "Execute Multiple BASH Shell Commands"
(One intermediate revision by one user not shown) | |||
Line 5: | Line 5: | ||
echo 2 | echo 2 | ||
− | (A) run several commands using the control operator ";" (semicolon) which will execute them sequentially. The shell will wait for each command to terminate in turn. The return status is the exit status of the last command executed. | + | (A) SEQUENTIAL: run several commands using the control operator ";" (semicolon) which will execute them sequentially. The shell will wait for each command to terminate in turn. The return status is the exit status of the last command executed. |
echo 1; sleep 5s; echo 2 | echo 1; sleep 5s; echo 2 | ||
− | (B) run several commands using the control operator "&" (ampersand) | + | (B) SIMULTANEOUS: run several commands using the control operator "&" (ampersand) which will allow executing of all commands simultaneously. |
echo 1 & sleep 5s & echo 2 | echo 1 & sleep 5s & echo 2 | ||
− | (C) run several commands using double ampersands | + | (C) SEQUENTIAL CONDITIONAL: run several commands using double ampersands will prevent subsequent commands in the line from running of the preceding exits with exit status 1. Use the double-ampersand operator in BASH will provide conditional execution. Two commands separated by the double ampersands tells the shell to run the first command and then run the second command only if the first command succeeds with an exit status 0. It will behave like example (A) in that the shell will wait for each command to terminate, with the exception that the command must terminate successfully to continue with the next. |
echo 1 && sleep 5s && echo 2 | echo 1 && sleep 5s && echo 2 | ||
+ | |||
+ | Hopefully this provides some basic clarification of how the bash shell handles multiple commands per line. |
Latest revision as of 15:15, 2 October 2019
How to run shell commands sequentially or at the same time entered on a single line.
echo 1 sleep 5s echo 2
(A) SEQUENTIAL: run several commands using the control operator ";" (semicolon) which will execute them sequentially. The shell will wait for each command to terminate in turn. The return status is the exit status of the last command executed.
echo 1; sleep 5s; echo 2
(B) SIMULTANEOUS: run several commands using the control operator "&" (ampersand) which will allow executing of all commands simultaneously.
echo 1 & sleep 5s & echo 2
(C) SEQUENTIAL CONDITIONAL: run several commands using double ampersands will prevent subsequent commands in the line from running of the preceding exits with exit status 1. Use the double-ampersand operator in BASH will provide conditional execution. Two commands separated by the double ampersands tells the shell to run the first command and then run the second command only if the first command succeeds with an exit status 0. It will behave like example (A) in that the shell will wait for each command to terminate, with the exception that the command must terminate successfully to continue with the next.
echo 1 && sleep 5s && echo 2
Hopefully this provides some basic clarification of how the bash shell handles multiple commands per line.