Skip to content

Instantly share code, notes, and snippets.

@a-magdy
Last active November 13, 2023 14:01
Show Gist options
  • Select an option

  • Save a-magdy/0f6df1e0063efea9ab97bb9bc0c77bfa to your computer and use it in GitHub Desktop.

Select an option

Save a-magdy/0f6df1e0063efea9ab97bb9bc0c77bfa to your computer and use it in GitHub Desktop.

Revisions

  1. a-magdy revised this gist Nov 13, 2023. 1 changed file with 47 additions and 0 deletions.
    47 changes: 47 additions & 0 deletions source_env_var_file
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    # Export all variables from a env var file
    # $1 => env var file path
    source_env_var_file() {
    file="$1"

    if [ ! -f "$file" ]; then
    echo "Error: $file does not exist"
    return 1
    fi

    while IFS= read -r line; do
    # Trim whitespace from start and end of line
    line=$(echo "$line" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')

    # Validate the line against the regex (skips empty lines and comments and lines where key is not valid)
    [[ ! "$line" =~ ^[A-Za-z_][A-Za-z0-9_]*=.* ]] && continue
    # # Skip empty lines
    # [ -z "$line" ] && continue
    # # Skip comments
    # [[ "$line" =~ ^#.*$ ]] && continue

    # Split the line into a key and value
    key=$(echo "$line" | cut -d "=" -f 1)
    value=$(echo "$line" | cut -d "=" -f 2-)

    # Trim whitespace from start and end of value
    value=$(echo "$value" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')

    # Skip if the key or value is empty
    [ -z "$key" ] || [ -z "$value" ] && continue

    # If the value isn't wrapped in quotes and contains any special characters, wrap it in single quotes
    if [[ "$value" != "'"* && "$value" != '"'* ]] && [[ "$value" =~ [[:space:]\!\@\#\$\%\^\&\*\(\)\[\]\{\}\<\>\?\!\~\`\:\;\+\=\-\.\/\\\|] ]]; then
    value="'$value'"
    fi

    # Export the variable
    export "$key"="$value"
    done <"$file"

    # # eval "$(grep -E '^export [A-Za-z_][A-Za-z0-9_]*=.*' "$file")" # validates lines with regex as (export key=value) and evals the line
    # eval "$(grep -E '^[A-Za-z_][A-Za-z0-9_]*=.*' "$file" | sed 's/.*/export &;/')" # normal approach - validate lines with regex as (key=value) and export them

    # Special chars to check: \s,!,@,#,$,%,^,&,*,(,),[,],{,},<,>,?,!,~,`,",',:,;,+,=,-,.,_,/,\,|
    # Wrap the value in single quotes if contains special chars and not already wrapped
    # eval "$(grep -E '^[A-Za-z_][A-Za-z0-9_]*=.*' "$file" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//; s/[^A-Za-z0-9_]=/=/; s/.*/&;/; s/=[^'"'"'A-Za-z0-9_]/='\''&'\''/')"
    }
  2. a-magdy renamed this gist Aug 31, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. a-magdy renamed this gist Aug 31, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. a-magdy revised this gist Aug 31, 2023. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions .env
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # Example .env file




    VAR=something
    VAR2='one two three'
  5. a-magdy revised this gist Aug 31, 2023. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion scripts.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    #!/bin/bash

    # send the file to grep directly
    # Use grep to validate each line using regex (clears out comments, empty lines and validates that each line is in the format of env vars)
    # Then send it out to sed, to append export at the beginning of each line and ; at the end
    # Then eval all
    eval "$(grep -E '^[A-Za-z_][A-Za-z0-9_]*=.*' '.env' | sed 's/.*/export &;/')"

    #====================================================================================
  6. a-magdy revised this gist Aug 31, 2023. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions scripts.sh
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,16 @@
    #!/bin/bash

    # send the file to grep directly
    eval "$(grep -E '^[A-Za-z_][A-Za-z0-9_]*=.*' '.env' | sed 's/.*/export &;/')"

    #====================================================================================
    # Archive
    #====================================================================================
    # Simple (Works for most cases)
    export $(grep -v '^#' .env | xargs)

    # Complex (Works with vars that contain spaces
    eval "$(cat '.env' | grep -v '^#' | grep -v '^$' | sed 's/.*/export &;/')"

    # Complex with 1 grep regex that covers all cases (comments, empty lines & validates the <var_name=...> style)
    eval "$(cat './.env.local' | grep -E '^[A-Za-z_][A-Za-z0-9_]*=.*' | sed 's/.*/export &;/')"

    #====================================================================================
  7. a-magdy revised this gist Aug 31, 2023. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions scripts.sh
    Original file line number Diff line number Diff line change
    @@ -5,3 +5,6 @@ export $(grep -v '^#' .env | xargs)

    # Complex (Works with vars that contain spaces
    eval "$(cat '.env' | grep -v '^#' | grep -v '^$' | sed 's/.*/export &;/')"

    # Complex with 1 grep regex that covers all cases (comments, empty lines & validates the <var_name=...> style)
    eval "$(cat './.env.local' | grep -E '^[A-Za-z_][A-Za-z0-9_]*=.*' | sed 's/.*/export &;/')"
  8. a-magdy created this gist Aug 31, 2023.
    7 changes: 7 additions & 0 deletions scripts.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    #!/bin/bash

    # Simple (Works for most cases)
    export $(grep -v '^#' .env | xargs)

    # Complex (Works with vars that contain spaces
    eval "$(cat '.env' | grep -v '^#' | grep -v '^$' | sed 's/.*/export &;/')"