Skip to content

kojiromike/parameter-expansion

Repository files navigation

POSIX Parameter Expansion

GitHub PyPI PyPI - Python Version PyPI - Wheel PyPI - Downloads

Tests CodeQL pre-commit

This is an experimental Python library to enable POSIX parameter expansion in a string. It supports also a subset of Bash parameter expansion.

Why not spawning a shell directly for this?

One reason is that it may be security risk. Another reason is to support lightweight analysis or evaluation of shell parameters with few system dependencies and outside of a running shell.

For instance this use in scancode-toolkit as part of a lightweight shell script parser to extract and expand parameters found in some build scripts.

Which expansions are supported?

All the standard shell expansions are supported, including some level of nested expansion, as long as this is not too complex or ambiguous. In addition, we support Bash substrings and string replacement. There is an extensive test suite listing all supported substitions

How does this work?

The expand() function accepts a string and a dictionary of variables (otherwise it uses the current environmnent variables). The string is parsed with a custom parser and interpreted to perform the various expansion procedures using these variables.

Obvious Test Cases

    >>> from parameter_expansion import expand
    >>> foo = 'abc/123-def.ghi'
    >>> # Bland Expansion
    >>> expand('abc $foo abc')
    'abc abc/123-def.ghi abc'
    >>> expand('abc${foo}abc')
    'abcabc/123-def.ghiabc'
    >>>
    >>> # Default Value Expansion
    >>> expand('-${foo:-bar}-')
    '-abc/123-def.ghi-'
    >>> expand('-${bar:-bar}-')
    '-bar-'

Default Value Expansion

    >>> foo = 'abc/123-def.ghi'
    >>> expand('abc $foo abc')
    'abc abc/123-def.ghi abc'
    >>> expand('abc${foo}abc')
    'abcabc/123-def.ghiabc'

Any other library doing similar thing?