Last active
November 13, 2023 14:01
-
-
Save a-magdy/0f6df1e0063efea9ab97bb9bc0c77bfa to your computer and use it in GitHub Desktop.
Revisions
-
a-magdy revised this gist
Nov 13, 2023 . 1 changed file with 47 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal 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_]/='\''&'\''/')" } -
a-magdy renamed this gist
Aug 31, 2023 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
a-magdy renamed this gist
Aug 31, 2023 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
a-magdy revised this gist
Aug 31, 2023 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ # Example .env file VAR=something VAR2='one two three' -
a-magdy revised this gist
Aug 31, 2023 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,8 @@ #!/bin/bash # 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 &;/')" #==================================================================================== -
a-magdy revised this gist
Aug 31, 2023 . 1 changed file with 8 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal 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 &;/')" #==================================================================================== -
a-magdy revised this gist
Aug 31, 2023 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal 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 &;/')" -
a-magdy created this gist
Aug 31, 2023 .There are no files selected for viewing
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 charactersOriginal 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 &;/')"