Best format for parameter expansion?

Many languages and document systems have a way to have formal variables in strings replaced by specific instance values. The dominant practice is to use braces, with some prefix character, often $. These are the most common, in order

  • ${PARAMETER}
  • {PARAMETER}
  • #{PARAMETER}

Other widely-used formats (where widely-used is by usage)

  • $PARAMETER
  • $(PARAMETER)

Some systems generalize PARAMETER to include arbitrary expressions, some have separate syntax for variable interpolation versus expression interpolation.

There is a Wikipedia page for String Interpolation, but it is incomplete/misleading. Maybe I should update it.

Suggestion for a new system? Use ${EXPRESSION} and allow at least simple variables, but preferably full expressions.

Perl

Perl calls it “interpolation” when you put variables inside strings. Perl allows all variable types to be expanded. In a string, it’s common practice to surround the variable name with braces to better disambiguate variable names from surrounding text.

my $name = "Brian";
my $action = "jump";
print "$name ${action}s\n";
# output: Brian jumps

It’s called interpolation more when it is about inserting arrays into strings, because array elements are as a convenience separated by spaces.

Many configuration systems (including Puppet and Nagios) have Perl-like syntax for string interpolation. PHP deviates a bit because it uses $PARAMETER for simple cases, and {$EXPRESSION} for more complicated ones.

Python

Python doesn’t have implicit expansion, but it has several systems for doing explicit expansion.

The format() function uses {PARAMETER} syntax.

>>> d = { 'vars': "variables", 'example': "example" }
>>> s = "This is an {example} with {vars}"
>>> s.format(**d)
'This is an example with variables'

There is a Template module that uses a $PARAMETER syntax

>>> from string import Template
>>> t = Template("This is an $example with $vars")
>>> t.substitute({ 'example': "example", 'vars': "variables"})
'This is an example with variables'

python multiline string for variables from StackOverflow

Ruby

Ruby has generalized interpolation of expressions into strings with #{EXPRESSION}, where the expression can be as simple as a single variable, or as complicated as you want.

Bash

Bash share’s Perls syntax for strings (or is it that Perl used Bash’s syntax?).

set WORD=jump
echo "The plural of $WORD is most likely ${WORD}s"
# output: The plural of jump is most likely jumps

Bash has a lot of decoration to parameter substitution driven by the fact that there is no higher-level language backing things up, so it has to have decoration for indirection, case change, substring, search/replace and so on; things that would all be done outside of parameter expansion in other systems.

This syntax probably goes back to the original /bin/sh shell, because the fundamentals of this are shared by csh/tcsh, and some other shells.

Bash parameter expansion syntax

zsh

The zsh shell does parameter expansion as $PARAMETER or ${PARAMETER}, and introduces a new syntax to do function expansion – $(func) calls func and inserts its output as text.

zsh is very similar to ksh.

A User’s Guide to the Z-Shell

Dart

Dart, Google’s replacement for Javascript, uses $PARAMETER for simple variables, and ${EXPRESSION} for arbitrary expressions. If EXPRESSION is a variable, then the toString() method is automatically called on that variable.

Dart String Interpolation

Microsoft

Batch files use %PARAMETER% for variable expansion, and have decorators for substring and substitution, except that batch files do immediate substitution, not delayed substitution (e.g. changing a variable at “run time”). There is special syntax to add delayed substitution, bringing it more in line with other systems; these are written as !PARAMETER!. Presumably this was due to fears of existing batch files depending on immediate substitution.

Microsoft tends to like $(PARAMETER) for its variable expansion syntax in newer systems. This is used by Visual Studio and MSBuild, among other systems.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>