Skip to main content
added 452 characters in body
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

Expansions don't get recursively applied. Doing that would make it impossible to handle arbitrary data with dollar signs embedded. (A related matter is that quotes and redirection and other operators are also just regular characters after expansions.)

A somewhat usual custom is to have config files like that as actual shell script, so (like the ones in Debian's /etc/default), so the file would be

DIR="${HOME}/test/tmp"

and you'd read it with

. configfile

though of course that has the problem that the file is a full shell script, must conform to the shell syntax and the config can run arbitrary commands.

Another possibility would be to run the file through envsubst, e.g. with the file in your question, envsubst < configfile would output:

DIR = "/home/me/test/tmp"

or you could use envsubst '$HOME $OTHER $VARS' to expand just some particular ones.

Note that unlike a shell script would do, envsubst doesn't read any assignments from the input file. E.g. the value of ROOTPATH used on the second line is the one envsubst gets from the environment, it has nothing to do with the "assignment" on the first line:

ROOTPATH=/something/$USER
LOGPATH=$ROOTPATH/logs

Expansions don't get recursively applied. Doing that would make it impossible to handle arbitrary data with dollar signs embedded.

A somewhat usual custom is to have config files like that as actual shell script, so (like the ones in Debian's /etc/default), so the file would be

DIR="${HOME}/test/tmp"

and you'd read it with

. configfile

though of course that has the problem that the file is a full shell script, must conform to the shell syntax and the config can run arbitrary commands.

Another possibility would be to run the file through envsubst, e.g. with the file in your question, envsubst < configfile would output:

DIR = "/home/me/test/tmp"

or you could use envsubst '$HOME $OTHER $VARS' to expand just some particular ones.

Expansions don't get recursively applied. Doing that would make it impossible to handle arbitrary data with dollar signs embedded. (A related matter is that quotes and redirection and other operators are also just regular characters after expansions.)

A somewhat usual custom is to have config files like that as actual shell script, so (like the ones in Debian's /etc/default), so the file would be

DIR="${HOME}/test/tmp"

and you'd read it with

. configfile

though of course that has the problem that the file is a full shell script, must conform to the shell syntax and the config can run arbitrary commands.

Another possibility would be to run the file through envsubst, e.g. with the file in your question, envsubst < configfile would output:

DIR = "/home/me/test/tmp"

or you could use envsubst '$HOME $OTHER $VARS' to expand just some particular ones.

Note that unlike a shell script would do, envsubst doesn't read any assignments from the input file. E.g. the value of ROOTPATH used on the second line is the one envsubst gets from the environment, it has nothing to do with the "assignment" on the first line:

ROOTPATH=/something/$USER
LOGPATH=$ROOTPATH/logs
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

Expansions don't get recursively applied. Doing that would make it impossible to handle arbitrary data with dollar signs embedded.

A somewhat usual custom is to have config files like that as actual shell script, so (like the ones in Debian's /etc/default), so the file would be

DIR="${HOME}/test/tmp"

and you'd read it with

. configfile

though of course that has the problem that the file is a full shell script, must conform to the shell syntax and the config can run arbitrary commands.

Another possibility would be to run the file through envsubst, e.g. with the file in your question, envsubst < configfile would output:

DIR = "/home/me/test/tmp"

or you could use envsubst '$HOME $OTHER $VARS' to expand just some particular ones.