Skip to content

Main content

Environment Literal Reference Substitution

Substitute simple literal environment references once using explicit last-wins values, preserve unknown references and do not recursively expand inserted dollar text.

Loading tool…

Description

Substitute simple literal environment references once using explicit last-wins values, preserve unknown references and do not recursively expand inserted dollar text.

Environment assignments pair names with literal string values. Repeated names need an explicit first/last policy, and missing values differ from present empty strings. A dotenv file and a shell program can use different quoting and expansion rules; these tools implement their stated literal subsets using supplied data.

Substitute simple literal environment references once using explicit last-wins values, preserve unknown references and do not recursively expand inserted dollar text.

When to use this tool

Use this tool to substitute simple literal environment references once using explicit last-wins values, preserve unknown references and do not recursively expand inserted dollar text.

How to use the tool and how it works

  1. Enter text, assignmentsJson using the input definitions below. Serialize the JSON text fields in the decoded shape shown below.
  2. Run the operation. Resolve malformed-input errors before interpreting the result.
  3. Read result.text, result.unknown. Interpret signs, units, ordering and null values using the rules below.

Read case-sensitive {name,value} assignments into a last-wins map. Replace recognized original references once, preserving each unknown reference’s exact spelling, and return distinct unknown names in first-appearance order.1

Input fieldMeaning and unitsAccepted input
textLiteral text to scan for the stated simple dollar-reference syntax.Text, at most 8,192 UTF-16 code units; the stated grammar and narrower limits also apply.
assignmentsJsonJSON array of {name, value} strings using portable ASCII names and NUL-free literal values. Repeated names are retained until the stated policy is applied.Text, at most 8,192 UTF-16 code units; the stated grammar and narrower limits also apply.

These decoded examples show the structure of the JSON text fields. Supply each as its corresponding serialized JSON string.

json
{
  "assignmentsJson": [
    {
      "name": "A",
      "value": "$B"
    }
  ]
}
Output fieldInterpretation
resultObject containing the fields below.
result.textOnce-substituted literal text, preserving unknown reference spellings.
result.unknownDistinct unknown reference names in first-appearance order.
json
{
  "input": {
    "text": "$A ${B}",
    "assignmentsJson": "[{\"name\":\"A\",\"value\":\"$B\"}]"
  },
  "output": {
    "result": {
      "text": "$B ${B}",
      "unknown": [
        "B"
      ]
    }
  }
}

Limitations and assumptions

  • Inserted values are literal and are never recursively expanded. No process environment is read or changed.
  • Portable assignment records require names matching [A-Za-z_][A-Za-z0-9_]* and NUL-free values. Name matching is case-sensitive. Reference scanners and quoted-value helpers follow only their own stated grammar and do not read the process environment.
  • Every declared top-level input is required and unknown fields are rejected. Ordinary strings are limited to 8,192 UTF-16 code units and lists to 128 entries, with tighter operation-specific bounds where stated. JSON text has bounded nesting and node counts.
  • Numeric outputs must be finite and within the absolute safe-integer magnitude; exact larger integers use decimal text. Source positions are zero-based UTF-16 offsets and span ends are exclusive unless a different convention is explicitly stated.

Alternative or Complementary methods

Select the consuming application’s dotenv dialect explicitly. Keep literal substitution separate from recursive expansion and shell execution, and retain occurrence provenance when resolving duplicate assignments.

References

  1. Tool behavior and example: Environment Literal Reference Substitution — tool.io

Similar or alternative tools