Linux中可以使用分号“;”、双and号“&&”和双竖线“||”来连接多个命令。单"&"符号也算命令连接符号,只不过它是将其前面的命令放入后台执行,所以可以变相地实现命令并行执行。
1.分号";"
command1 ; command2
命令之间没有逻辑关系。分号连接的命令会按照顺序从前向后依次执行,但分号两端的命令之间没有任何逻辑关系,所有写出来的命令最终都会被执行,即使分号前面的命令出错也不影响后面的命令。
[root@xuexi ~]# ls das;echo "hdakl" ls: cannot access das: No such file or directory hdakl
2.&&
command1 && command2
逻辑与。&&连接的命令会按照顺序从前向后执行,但只有当command1正确执行才执行command2,如果command1不正确执行,则不执行command2。在bash中,通过预定义变量“$?”来判断命令是否正确执行,如果"$?"的值为0则表示前一条命令正确执行,其他任意值都表示不正确执行。\
[root@xuexi ~]# echo "hdakl" && ls ds hdakl ls: cannot access ds: No such file or directory
[root@xuexi ~]# ls das && echo "hdakl" ls: cannot access das: