English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
If each command is separated by ";", then regardless of whether the previous command executed successfully or not, the next command will continue to execute
Here, the second command's echo has an extra 'o' intentionally written, causing the command to execute with an error, but it does not affect the execution of subsequent commands
You can think of it this way, if commands are separated by a semicolon, it is equivalent to separating the commands on different lines, and whether the command on the previous line is successful or not will not affect the execution of the next line command.
$ echo 1; echoo 2; echo 3; echo 4
1
-bash: echoo: command not found
3
4
If commands are separated by "&&", then only the command that executed successfully will continue to execute the subsequent command
Here, the second command's echo has an extra 'o' intentionally written, causing the command to execute with an error, echo 3It did not execute, therefore echo4was not executed
$ echo 1 && echoo 2 && echo 3 && echo 4
1
-bash: echoo: command not found
if the commands are separated by "||", then only the command before it fails to continue executing the following command
here echo 1if the commands are separated by "||", then only the command before it fails to continue executing the following command 2, then echo was not executed 3, echo 4
echo 1 || echo 2 || echo 3 || echo 4
1
here echoo 1failed to execute, so started to execute echo 2, execution was successful, so the following echo 3, echo4were not executed
echoo 1 || echo 2 || echo 3 || echo 4
-bash: echoo: command not found
2
Based on the above rules, analyze several special examples, that is, the examples mixed with a variety of separators
echo 1was executed successfully, followed by two "||", so echo 2, echo 3were not executed, afterwards encountered "&&", and the command combination before it was considered to be executed successfully, so echo 4was executed
$ echo 1 || echo 2 || echo 3 && echo 4
1
4
echo 1was executed successfully, followed by echoo 2, execution failed, therefore echo 3was not executed, but afterwards encountered "||", and the previous command combination was considered to be executed failed, so echo 4was executed
$ echo 1 && echoo 2 && echo 3 || echo 4
1
-bash: echoo: command not found
4
echo 1was executed successfully, followed by echoo 2, execution failed, therefore echo 3was not executed, but afterwards encountered ";", which is equivalent to placing the following command on a new line, so no matter how echo 4was executed
$ echo 1 && echoo 2 && echo 3 ; echo 4
1
-bash: echoo: command not found
4
echoo 1failed to execute, followed by "||", so echo 2was executed, echo 2was executed successfully, followed by two "||", so echo 3, echo 4was not executed, but afterwards encountered "&&", and the previous command combination was considered to be executed successfully, so echoo5was executed, the execution occurred with an error, so echo 6was not executed, but there is a ";" afterwards, so no matter how echo 7will be executed
$ echoo 1 || echo 2 || echo 3 || echo 4 && echoo 5 && echo 6 ; echo 7
-bash: echoo: command not found
2
-bash: echoo: command not found
7
The method of executing multiple commands in a row in Linux (recommended) that I shared with you is all the content I have to share with you. I hope it can provide you with a reference, and I also hope everyone will support the yanaojiao.cn tutorial.