这是我脚本的内容
#!/bin/bash
# test.sh
# Note: Set permissions on this script to 555 or 755,
# then call it with ./test.sh or sh ./test.sh.
echo
echo "This line appears ONCE in the script, yet it keeps echoing."
echo "The PID of this instance of the script is still $$."
# Demonstrates that a subshell is not forked off.
echo "==================== Hit Ctl-C to exit ===================="
sleep 1
exec $0 # Spawns another instance of this same script
#+ that replaces the previous one.
echo "This line will never echo!" # Why not?
exit 99 # Will not exit here!
# Exit code will not be 99!
这是我使用 /bin/bash 运行脚本时的输出
[user@localhost ~]$ /bin/bash test.sh
This line appears ONCE in the script, yet it keeps echoing.
The PID of this instance of the script is still 4872.
==================== Hit Ctl-C to exit ====================
test.sh: line 11: exec: test.sh: not found
这是我使用 /bin/sh 运行脚本时的输出
[user@localhost ~]$ /bin/sh ./test.sh
This line appears ONCE in the script, yet it keeps echoing.
The PID of this instance of the script is still 4934.
==================== Hit Ctl-C to exit ====================
This line appears ONCE in the script, yet it keeps echoing.
The PID of this instance of the script is still 4934.
==================== Hit Ctl-C to exit ====================
我不得不使用 Ctl-C 来阻止它。
为什么相同的脚本会根据不同的执行模式表现出不同的行为。仅供参考:我必须使用 Ctl-C 执行脚本,如下所示:./test.sh