Desk Clarity

A field guide to everyday digital problems.

Developer tools / Practical guide

Bash Prefix Assignment Changes the Child Environment, Not Earlier Argument Expansion

In a command such as LABEL=new printf ...

By ON Lab2 min readSources checked November 10, 2026 (KST)

In a command such as LABEL=new printf ... "$LABEL", the argument expansion can still use the current shell value. A temporary assignment for the invoked command is a different boundary from expanding the command line in the parent shell.

Reproduce in an empty temporary directory

This isolated example creates only fixture data in its working directory or runs local JavaScript. Run it separately from your project so the named files cannot overwrite real work.

export LABEL=old
LABEL=new printf 'argument=%s\n' "$LABEL"
LABEL=new bash -c 'printf "child=%s\n" "$LABEL"'
printf 'parent=%s\n' "$LABEL"
test "$LABEL" = old

Observed local output

argument=old
child=new
parent=old

What to compare

The first command prints argument=old. The explicit child shell reads its own environment and prints child=new. The final command shows parent=old. Single quotes around the child program prevent the parent from expanding its dollar expression before Bash starts. The fixture separates the supplied argument from what the child later reads.

Choose the next action

If a command needs a newly assigned value as an argument, assign it in a separate statement and then expand it, using a subshell when the change should remain scoped. If a program reads a configuration value from its environment, use the prefix assignment and let that program read the variable directly. Do not build an extra sh -c layer solely to hide which process is responsible for expansion; direct program arguments are usually easier to review.

Limits and verification

This article demonstrates an external child shell and Bash builtins, not every special builtin or POSIX mode. Assignment-only commands and shell functions have additional persistence rules. The fixture uses a harmless temporary name inside a disposable process. For pipeline commands, verify which stage receives the environment assignment instead of assuming it configures the entire pipeline.

Related: A Bash Heredoc Expands Template Variables Too Early: Quote the Delimiter.

Isolated example executed on macOS on November 10, 2026 (KST), using Bash 3.2, Python 3 for the byte fixture, and Node v26.5.0. Results describe the example, not your application or every runtime version.

Sources & further reading