Skip to main content
added 60 characters in body
Source Link
goulashsoup
  • 199
  • 1
  • 13

To extend upon the Ulrich Schwarzs answer, providing a solution that should also work for "special" kinds of environment variables, I use awk with ENVIRON (answer given by dave_thompson_085):

$ echo 'Hello $USER $UNDEFINED_VARIABLE' | envsubst "$(awk 'BEGIN{for(e in ENVIRON)print "$"e}' | awk '{ printf("%s ", $0) }')"
Hello <user> $UNDEFINED_VARIABLE

In script:

environment_variables_single_line="$(awk 'BEGIN{for(e in ENVIRON)print "$"e}' | awk '{ printf("%s ", $0) }')"
envsubst "${environment_variables_in_single_line}" < ${source_file} > ${target_file}

To extend upon the Ulrich Schwarzs answer, providing a solution that should also work for "special" kinds of environment variables, I use awk with ENVIRON (answer given by dave_thompson_085):

$ echo 'Hello $USER $UNDEFINED_VARIABLE' | envsubst "$(awk 'BEGIN{for(e in ENVIRON)print "$"e}')"
Hello <user> $UNDEFINED_VARIABLE

In script:

environment_variables_single_line="$(awk 'BEGIN{for(e in ENVIRON)print "$"e}')"
envsubst "${environment_variables_in_single_line}" < ${source_file} > ${target_file}

To extend upon the Ulrich Schwarzs answer, providing a solution that should also work for "special" kinds of environment variables, I use awk with ENVIRON (answer given by dave_thompson_085):

$ echo 'Hello $USER $UNDEFINED_VARIABLE' | envsubst "$(awk 'BEGIN{for(e in ENVIRON)print "$"e}' | awk '{ printf("%s ", $0) }')"
Hello <user> $UNDEFINED_VARIABLE

In script:

environment_variables_single_line="$(awk 'BEGIN{for(e in ENVIRON)print "$"e}' | awk '{ printf("%s ", $0) }')"
envsubst "${environment_variables_in_single_line}" < ${source_file} > ${target_file}
deleted 166 characters in body
Source Link
goulashsoup
  • 199
  • 1
  • 13

To extend upon the Ulrich Schwarzs answer, providing a solution that should also work for "special" kinds of environment variables, I use declare -xpawk to get the environment variables, as describedwith hereENVIRON (answer given by dave_thompson_085):

$ echo 'Hello $USER $UNDEFINED_VARIABLE' | envsubst "$(declare -xp | cut -d= -f1 | awk ''BEGIN{ subfor(/^declare -xe /,in ""ENVIRON); print }' | awk '{ print "$" $0 }' | awk '{ printf("%s ", $0) "$"e}')"
Hello <user> $UNDEFINED_VARIABLE

In script:

environment_variables_single_line="$(declare -xp | cut -d= -f1 | awk ''BEGIN{ subfor(/^declare -xe /,in ""ENVIRON); print }' | awk '{ print "$" $0 }' | awk '{ printf("%s ", $0) "$"e}')"
envsubst "${environment_variables_in_single_line}" < ${source_file} > ${target_file}

To extend upon the Ulrich Schwarzs answer, providing a solution that should also work for "special" kinds of environment variables, I use declare -xp to get the environment variables, as described here:

$ echo 'Hello $USER $UNDEFINED_VARIABLE' | envsubst "$(declare -xp | cut -d= -f1 | awk '{ sub(/^declare -x /, ""); print }' | awk '{ print "$" $0 }' | awk '{ printf("%s ", $0) }')"
Hello <user> $UNDEFINED_VARIABLE

In script:

environment_variables_single_line="$(declare -xp | cut -d= -f1 | awk '{ sub(/^declare -x /, ""); print }' | awk '{ print "$" $0 }' | awk '{ printf("%s ", $0) }')"
envsubst "${environment_variables_in_single_line}" < ${source_file} > ${target_file}

To extend upon the Ulrich Schwarzs answer, providing a solution that should also work for "special" kinds of environment variables, I use awk with ENVIRON (answer given by dave_thompson_085):

$ echo 'Hello $USER $UNDEFINED_VARIABLE' | envsubst "$(awk 'BEGIN{for(e in ENVIRON)print "$"e}')"
Hello <user> $UNDEFINED_VARIABLE

In script:

environment_variables_single_line="$(awk 'BEGIN{for(e in ENVIRON)print "$"e}')"
envsubst "${environment_variables_in_single_line}" < ${source_file} > ${target_file}
Source Link
goulashsoup
  • 199
  • 1
  • 13

To extend upon the Ulrich Schwarzs answer, providing a solution that should also work for "special" kinds of environment variables, I use declare -xp to get the environment variables, as described here:

$ echo 'Hello $USER $UNDEFINED_VARIABLE' | envsubst "$(declare -xp | cut -d= -f1 | awk '{ sub(/^declare -x /, ""); print }' | awk '{ print "$" $0 }' | awk '{ printf("%s ", $0) }')"
Hello <user> $UNDEFINED_VARIABLE

In script:

environment_variables_single_line="$(declare -xp | cut -d= -f1 | awk '{ sub(/^declare -x /, ""); print }' | awk '{ print "$" $0 }' | awk '{ printf("%s ", $0) }')"
envsubst "${environment_variables_in_single_line}" < ${source_file} > ${target_file}