| 1 | #!/bin/bash |
| 2 | # self-exec.sh |
| 3 | |
| 4 | # Note: Set permissions on this script to 555 or 755, |
| 5 | # then call it with ./self-exec.sh or sh ./self-exec.sh. |
| 6 | |
| 7 | echo |
| 8 | |
| 9 | echo "This line appears ONCE in the script, yet it keeps echoing." |
| 10 | echo "The PID of this instance of the script is still $$." |
| 11 | # Demonstrates that a subshell is not forked off. |
| 12 | |
| 13 | echo "==================== Hit Ctl-C to exit ====================" |
| 14 | |
| 15 | sleep 1 |
| 16 | |
| 17 | exec $0 # Spawns another instance of this same script |
| 18 | #+ that replaces the previous one. |
| 19 | |
| 20 | echo "This line will never echo!" # Why not? |
| 21 | |
| 22 | exit 99 # Will not exit here! |
| 23 | # Exit code will not be 99! |