在Bash中,可以使用如下两种方式在同一个进程中执行下一个命令:
&&:command1 && command2
这将会在执行完 command1 后,只有在其成功执行时才会执行 command2。
示例代码:
#!/bin/bash
echo "Starting script"
echo "Executing command1"
command1 && echo "command1 executed successfully"
echo "Executing command2"
command2 && echo "command2 executed successfully"
echo "Script completed"
;:command1 ; command2
这将会依次执行 command1 和 command2,无论前一个命令是否成功执行。
示例代码:
#!/bin/bash
echo "Starting script"
echo "Executing command1"
command1 ; echo "command1 executed"
echo "Executing command2"
command2 ; echo "command2 executed"
echo "Script completed"
请根据具体需求选择适合的方式。