Skip to main content

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.

Note that this is a fork from upstream to support proper release on PyPI.

This repo https://github.com/nexB/parameter_expansion_patched is released at https://pypi.org/project/parameter-expansion-patched/ on PyPI.

Upstream is less active lately at https://github.com/kojiromike/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?

Release files for parameter-expansion-patched 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for parameter-expansion-patched 0.3.1
File Size Uploaded
parameter-expansion-patched-0.3.1.tar.gz 14.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for parameter-expansion-patched 0.3.1
File Interpreter ABI Platform
parameter_expansion_patched-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 26.4 kB

Release files / parameter-expansion-patched-0.3.1.tar.gz

Download URL parameter-expansion-patched-0.3.1.tar.gz
Size 14.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ff5dbc89fbde582f3336562d196b710771e92baa7b6d59356a14b085a0b6740b
BLAKE2b-256 checksum
How to use checksums
7e150c6fa115b269418a0d53d4564809afb74684d8afa417323b406be26de08b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.10

Release files / parameter_expansion_patched-0.3.1-py3-none-any.whl

Download URL parameter_expansion_patched-0.3.1-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
832f04bed2a81e32d9d233cbe27448a7a22edf9a744086dbd01066c41ad0f535
BLAKE2b-256 checksum
How to use checksums
6c9f2eb2762808faed5218faba5559415b5bb62b39376cf9a38acc01f9786481
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.10
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page