This is an old revision of the document!
Bash scripts are not compiled into executable binary files, but are rather fed into the bash
interpreter which reads the script character by character from the input it is given. The input is parsed and tokenized, i.e. commands and control structures seperated from data, and finally executed. This character -based approach to interpreting the given input has the upside of having a small memory footprint, but the downside of being susceptible to code injection.
Example:
#!/bin/bash echo "This line will be only printed once!" padded=$(printf '%*s' $(sed -e 's/^[[:space:]]*//' $0 | wc -c) ' ' | cat - "$0") echo "$padded" > "$0"
When executed with:
user@host:~$ ./code_injection.sh This line will be only printed once! This line will be only printed once! This line will be only printed once! [...]
the above script rewrites itself, shifting the start of the script content to the position after the last previous content.