question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proper Variable Expansion

See original GitHub issue

Currently, dotbot uses os.path.expandvars. However, in some cases, it may be desireable to follow proper shell parameter substitution. (see: the related manual) Here is an example usage: I want my zshrc to work. Some systems may define $ZDOTDIR to be $XDG_DATA_HOME/zsh, some may leave it undefined. The typical solution would be to use ${ZDOTDIR:-$HOME}/.zshrc as the install location. However, without parameter substitution, there is no way to achieve this.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:27 (13 by maintainers)

github_iconTop GitHub Comments

3reactions
simonvanderveldtcommented, Dec 19, 2021

FYI there would definitely be more projects that could use a way to use a POSIX compatible variable substitution, most of the time the main reason is being able to define a default value. Example of a project that’s looking for a solution to this docker/compose#1377

3reactions
anishathalyecommented, Nov 13, 2015

I’d prefer to write an implementation from scratch in pure Python.

For reference, here’s the implementation of posixpath.expandvars from Python 3.4:

# Expand paths containing shell variable substitutions.
# This expands the forms $variable and ${variable} only.
# Non-existent variables are left unchanged.

_varprog = None
_varprogb = None

def expandvars(path):
    """Expand shell variables of form $var and ${var}.  Unknown variables
    are left unchanged."""
    global _varprog, _varprogb
    if isinstance(path, bytes):
        if b'$' not in path:
            return path
        if not _varprogb:
            import re
            _varprogb = re.compile(br'\$(\w+|\{[^}]*\})', re.ASCII)
        search = _varprogb.search
        start = b'{'
        end = b'}'
        environ = getattr(os, 'environb', None)
    else:
        if '$' not in path:
            return path
        if not _varprog:
            import re
            _varprog = re.compile(r'\$(\w+|\{[^}]*\})', re.ASCII)
        search = _varprog.search
        start = '{'
        end = '}'
        environ = os.environ
    i = 0
    while True:
        m = search(path, i)
        if not m:
            break
        i, j = m.span(0)
        name = m.group(1)
        if name.startswith(start) and name.endswith(end):
            name = name[1:-1]
        try:
            if environ is None:
                value = os.fsencode(os.environ[os.fsdecode(name)])
            else:
                value = environ[name]
        except KeyError:
            i = j
        else:
            tail = path[j:]
            path = path[:i] + value
            i = len(path)
            path += tail
    return path

So it’s not way too complicated, but it’s not super simple either.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Shell Parameter Expansion (Bash Reference Manual) - GNU.org
Bash uses the value formed by expanding the rest of parameter as the new parameter ; this is then expanded and that value...
Read more >
Variables and Parameters in the Korn Shell - InformIT
Variable Expansion. Variable expansion is the term used for the ability to access and manipulate values of variables and parameters.
Read more >
An introduction to parameter expansion in Bash
Get started with this quick how-to guide on expansion modifiers that transform Bash variables and other parameters into powerful tools ...
Read more >
Variables and Expansions - The Bash Guide
Parameters can be expanded to inline their data into a command's arguments. Parameter expansion is done by prefixing the variable name with a...
Read more >
3.4. Shell expansion
Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found