{"id":2497,"library":"expandvars","title":"Expand Environment Variables Unix Style","description":"The `expandvars` Python library provides functionality to expand system environment variables within strings, similar to how Unix shells do. It supports both `$VAR` and `${VAR}` syntaxes, including default values and escape sequences. The current version is 1.1.2, with releases occurring periodically to address bugs and add features.","status":"active","version":"1.1.2","language":"en","source_language":"en","source_url":"https://github.com/sayanarijit/expandvars","tags":["environment variables","config","shell-like","expansion","unix"],"install":[{"cmd":"pip install expandvars","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"expandvars","correct":"from expandvars import expandvars"}],"quickstart":{"code":"import os\nfrom expandvars import expandvars\n\n# Set environment variables for demonstration (use os.environ.get for robustness)\nos.environ['API_KEY'] = os.environ.get('API_KEY', 'your_default_api_key')\nos.environ['SERVICE_URL'] = 'https://example.com/api'\n\n# Basic variable expansion\nconfig_string = \"My service at ${SERVICE_URL} requires key: $API_KEY\"\nexpanded_config = expandvars(config_string)\nprint(f\"Original: {config_string}\")\nprint(f\"Expanded: {expanded_config}\")\n\n# Example with a default value for an unset variable\nmissing_var_string = \"Optional setting: ${OPTIONAL_VAR:-default_value}\"\nexpanded_missing = expandvars(missing_var_string)\nprint(f\"Original: {missing_var_string}\")\nprint(f\"Expanded: {expanded_missing}\")\n\n# Example with escaping a dollar sign\nescaped_string = expandvars(\"Literal dollar sign: \\$VAR\")\nprint(f\"Escaped: {escaped_string}\")","lang":"python","description":"Demonstrates basic expansion of environment variables (`$VAR`, `${VAR}`), default values (`${VAR:-default}`), and escaping with a backslash (`\\$`)."},"warnings":[{"fix":"Upgrade to `expandvars` v1.1.2 or later to ensure correct handling of unset variable expansions.","message":"Prior to version 1.1.2, `expandvars` could incorrectly handle expansions involving unset variables, potentially leading to incorrect string parsing or unexpected output, especially when default values were used or mixed with other expansions.","severity":"gotcha","affected_versions":"<=1.1.1"},{"fix":"Review usage of `expandvars` with `disable_escape=True` or `force_braces=True` in v1.1.1+ to explicitly control parsing, especially if migrating from older versions or handling complex strings with many `$` characters.","message":"Before v1.1.1, the library's escaping behavior for `$` characters was fixed, and there was no explicit option to force the `${var}` syntax. If your strings contain literal `$` signs that were implicitly handled, or if you prefer strict `${var}` syntax, the new `disable_escape` and `force_braces` parameters might change or clarify behavior you previously relied on.","severity":"gotcha","affected_versions":"<=1.1.0"},{"fix":"Use `\\$` for a literal dollar sign. For example, `expandvars('My PID is \\$$')` will output 'My PID is $'.","message":"Unlike some Unix shells, `expandvars` does not interpret `$$` as the process ID (PID). Instead, `\\$` is the explicit escape sequence for a literal dollar sign. Be careful not to confuse this with shell behavior if you expect `$$` to expand to a PID.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}