[Python-ideas] String interpolation: environment variables, command substitution

Mike Miller python-ideas at mgmiller.net
Wed Aug 26 20:20:15 CEST 2015

One of the remaining questions for the string interpolation subject is whether 
to allow for easy access to environment variables and output-capture of external 
processes (aka command-substitution) as bash does.

While incredibly useful in use-cases such as shell-script replacements, the 
functionality is perceived to be, if not dangerous.  More so than arbitrary 
expressions?  Given that we are talking about string literals and not input, I'm 
not sure, so am looking for feedback.

The idea is not unheard of in Python, there was a module that captured process 
output called commands in the old days, which was superseded at some point by 
subprocess.check_output() I believe.

Here is some example syntax modeled on bash, though placed inside .format 
braces.  Note both start with $ as the signal::

     >>> x'Home folder: {$HOME}'      # environment
     'Home folder: /home/nobody'

     >>> x'Files: {$(/bin/ls .)}'     # capture output
     'foo foo1 foo2'

For safety, command substitution should return output in a way analogous to the 
modern equivalent::

     subprocess.check_output(['/bin/ls', '.'], shell=False).decode(encoding)


More information about the Python-ideas mailing list