diff --git a/dotenv.sh b/dotenv.sh index b88c8dc..e2056c6 100755 --- a/dotenv.sh +++ b/dotenv.sh @@ -56,9 +56,14 @@ export_envs() { continue fi - value=$(eval echo "$temp") + # Strip any existing quotes + temp="${temp%[\'\"]}"; + temp="${temp#[\'\"]}"; + # Add new double quotes for interpolation + value=$(eval echo "\"$temp\""); + eval export "$key='$value'"; - echo "$key=$value" >> $GITHUB_ENV + echo "$key=$value" >> $GITHUB_ENV; done < $1 } diff --git a/tests/.env b/tests/.env index aa438ea..a5135eb 100644 --- a/tests/.env +++ b/tests/.env @@ -13,5 +13,7 @@ TEST_INTERPOLATION="$TEST_UNQUOTED d=4" TEST_EXISTING="new-value" # Test secrets override defaults TEST_DOTENV_OVERRIDES_DEFAULT="expected" +# Test special characters are escaped +TEST_SPECIAL_CHARACTER=special(character # Test that the last variable is parsed despite no trailing new line -TEST_NO_NEWLINE="still there" +TEST_NO_NEWLINE="still there" \ No newline at end of file diff --git a/tests/dotenv-test.sh b/tests/dotenv-test.sh index a458a08..388f2b0 100755 --- a/tests/dotenv-test.sh +++ b/tests/dotenv-test.sh @@ -22,6 +22,7 @@ main() { assert_equal "$TEST_NO_NEWLINE" 'still there' 'Testing parsing of last line' assert_equal "$TEST_DEFAULT_ENVFILE" 'expected' 'Test loading variables from default.env file' assert_equal "$TEST_DOTENV_OVERRIDES_DEFAULT" 'expected' 'Test .env variables override variables from default.env file' + assert_equal "$TEST_SPECIAL_CHARACTER" 'special(character' 'Testing special characters' TEST_NO_ENVFILE=`DOTENV_FILE=nonexistent.env ../dotenv.sh 2>&1` # Close stdout for this test assert_equal "$TEST_NO_ENVFILE" "nonexistent.env file not found" 'Test error message from missing .env file'