All my shell scripts start like this: This section removes all variables, functi...
Luzzy @meluzzy@woof.tech
All my shell scripts start like this:
This section removes all variables, functions and aliases with single letter identifiers:
```
i="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z \
a b c d e f g h i j k l m n o p q r s t u v w x y z"
eval "unset -f $i; unset $i; unalias $i" >/dev/null 2>&1; :
```
This sections prevents this from running on shells that do not support local:
```
(command -v local >/dev/null || exit;F(){ local i=1; };F;[ -z "$i" ] && exit) \
|| : ${E:?"This shell doesn't support local variables. This can't run on it."}
```
The original directory the script was called from:
`OG_PWD=$PWD`
This process id:
`PROC_PID=$$`
The first command used to execute this script, Eg `bash ./script.sh` would yield 'bash', doing `./script.sh` would yield 'script.sh'.
`PROC_COMM=$(ps -p $$ -o comm=)`
The location of this source file:
`SOURCE_FILE=$(readlink -f "$0") || : ${E:?readlink -f "$0" failed}`
The directory of the source file, it also `cd`s to it to make sure it exists and to make relative path imports easier.
SOURCE_PATH=$(dirname "$SOURCE_FILE") && cd "$SOURCE_PATH" || : ${E:?Failed cd}
The path to the current shell running the script:
`SHELL_PATH=$(x=$(command -v "$PROC_COMM" || command -v sh);readlink -f "$x")`