[Shell command] Expand only specified environment variables with envsubst

Created on November 28, 2026

You can limit which environment variables envsubst expands by passing the variable names you want to expand as an argument.

Example

First, create a template file named sample.template with the following contents.

Hello, ${NAME1}.
Good bye, ${NAME2}.

Set the environment variables NAME1 and NAME2 to strings.

export NAME1="suzuki"
export NAME2="tanaka"

Pass an argument to envsubst that specifies only NAME1 for expansion.

cat sample.template | envsubst '${NAME1}'

The output shows that only ${NAME1} was expanded.

Hello, suzuki.
Good bye, ${NAME2}.

You can also specify multiple variables by separating them with commas.

cat sample.template | envsubst '${NAME1},${NAME2}'
Shell command