How to run multiple Linux commands at once or run multiple Linux commands in one line. Running multiple commands in a single line Linux is easy, learn how:
How to Run Multiple Linux Commands in One Line
There are 3 ways to run multiple commands in one line in Linux:
- ; Command 1 ; Command 2 – This will run command 1 first and then command 2
- && Command 1 && Command 2 – This will command 2 only if command 1 ends successfully
- || Command 1 || Command 2 – This will command 2 only if command 1 fails
See below to learn the use in full details:
- Using ;
When you use ; no matter the first command (command 1) run successfully or not, always run the second command (command 2).
- Using &&
When you use && the second command (command 2) will run only when the first command (command 1) run successfully.
- Using ||
When you use || the only when the second command (command 2) will be executed only when the first command (command 1) fails to run.
Running more than 2 commands at once in one line
You can use ; to run multiple Linux commands in one line. Simply use semicolon (;) to add/combine multiple commands in single line. The following format can be used:
command 1; command 2; command 3
NOTE: The command 1 will run first, then command 2 will run and then the command 3 will run. The commands will run irrespective of whether the previous command has beene xecuted successfully or not.
Similarly you can use command 1 && command 2 && command 3.