Last active
April 6, 2022 03:42
-
-
Save rosenta/8a9bf8c004fbfb88596eab1ab76422cd to your computer and use it in GitHub Desktop.
Script to remove environment variables from list of whitelisted or valid environments to keep. maintain file name called env_to_keep.txt where each line will have environment variable names, each separated by new line.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| BASH | |
| BASH_ALIASES | |
| BASH_ARGC | |
| BASH_ARGV | |
| BASH_CMDS | |
| BASH_COMMAND | |
| BASH_LINENO | |
| BASH_SOURCE | |
| BASH_SUB | |
| BASH_SUBSHELL | |
| BASH_VERSINFO | |
| BASH_VERSION | |
| BASHOPTS | |
| BASHPID | |
| CHOWN_HOME | |
| DEBIAN_FRONTEND | |
| DIRSTACK | |
| GROUPS | |
| HISTCMD | |
| HOME | |
| IFS | |
| LANG | |
| LANGUAGE | |
| LC_ALL | |
| LINENO | |
| LINES | |
| OPTERR | |
| OPTIND | |
| OPTS | |
| OSTYPE | |
| PATH | |
| PIPESTATUS | |
| PPID | |
| PS4 | |
| PWD | |
| PYX_DIMENSIONS | |
| PYXTERM_DIMENSIONS | |
| RANDOM | |
| SECONDS | |
| SHELL | |
| SHELLOPTS | |
| SHLVL | |
| TERM | |
| UAGE | |
| UID | |
| _ | |
| _SPEC | |
| _SPEC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # usage: bash whitelist_env.sh env_to_keep.txt | |
| filename=$1 # argument file env_to_keep.txt | |
| # list down all vars | |
| list_of_env_vars=`compgen -v` | |
| echo $list_of_env_vars | |
| echo "Starting to whitelist environments from file $filename" | |
| # going through whitelisted environments and remove whitelisted envs from avaible envs | |
| while read p; do | |
| p=" $p " | |
| list_of_env_vars=`echo $list_of_env_vars | sed "s/$p/ /g"` | |
| done < $filename | |
| echo "End" | |
| echo "below list of envs will be removed." | |
| echo $list_of_env_vars | |
| # removing remaining enviroments | |
| unset $list_of_env_vars | |
| declare +x $list_of_env_vars |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment