Skip to content

Instantly share code, notes, and snippets.

@AlexAtkinson
Created May 10, 2023 18:09
Show Gist options
  • Save AlexAtkinson/f9086c4a88a28cfdce5dbb1b15e947ea to your computer and use it in GitHub Desktop.
Save AlexAtkinson/f9086c4a88a28cfdce5dbb1b15e947ea to your computer and use it in GitHub Desktop.
An explanation of how $FOO_ENV environment variable configurations work.

FOO_ENV Environment Variables

These variables, such as $GITHUB_ENV, $ENV0_ENV, etc., simply point at a file that is sourced to populate an environment with the contained code, much like a .bashrc file.

These enviornment variables, while convenient, present a great amount of difficulty if they're not understood. Errors like ambiguous redirect, or no such file or directory, are common. In the worst case they get deleted, in which case they'll have to be re-created with the default content.

Beyond the simple foobars that users might have with this convenience utility, they also present an additional challenge in that they may persist data that is deisrable to delete. Clearing this data is another challenge. See below for more.

Demo

$ # Create the environment variable
$ COOL_ENV=~/.COOL_ENV

$ # Populate the file with a key/value (variable)
$ echo "foo=bar" >> $COOL_ENV

$ # Check the file contents
$ cat $COOL_ENV
foo=bar

$ # Source the file
$ source $COOL_ENV

$ Show that foo is now set
$ echo $foo
bar

Unsetting & Clearing Previously Set Variables

This function is an example of one you might setup to make maintenance of variables easier. Note that this function itself can be placed in an _ENV file, with a bit of escaping.

function expunge_envar() {
  [[ $# -ne 2 ]] && echo "ERROR: Must provide exatly two args: the variable to sanitize, and the ENV variable or file to remove it from."
  local var=$1
  local env_file=$2
  
  # Unset the var
  unset $var
  sed -i "/${var}=/d" $env_file
}

expunge_envar foo $COOL_ENV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment