Do not edit shell scripts that are currently running

Editing a shell script that is currently running can cause it to fail or execute unexpected, potentially destructive commands.

As an example, I will use this script:

#!/bin/bash
echo start
sleep 10
#echo EVIL
echo finish

Now, if we change 10 to 1 in the script while it is running, commented command will be executed. This happens because bash shell reads the script line by line, counting offset in bytes. Removing 0 from a line shifts the offset to echo EVIL command, which is executed.


Available as video here


2021/03/06