Without using source
to load another Bash config/script file, what is the simplest way to find the value of a Bash variable that is defined in a different (temporary) file?
I have seen similar snippets using cat
+ while
, grep
, and even eval
but none seem very clean nor can be applied to this scenario it seems...
For example:
update.sh
wants to download and verify the value of the variable LATEST
in a file /tmp/config
before deciding if it should proceed with the update process.
Updated (adding more context):
#!/bin/bash
## current config (includes an existing CONFIG_BUILD variable) ##
source config
## compability for updating config ##
UPDATER_BUILD="FEB2020"
## retrieve latest version of config online ##
wget -O /tmp/config http://mirrors.example.com/config-boilerplate
## ensure retrieved config boilerplate is compatible with current updater ##
CONFIG_LATEST=$(source /tmp/config; echo $CONFIG_BUILD)
if [[ "$CONFIG_LATEST" != "$UPDATER_BUILD" ]]; then
echo -e "Sorry, this updater is outdated or incompatible."
exit 1
fi
Updated (again): FML, I was missing the semicolon in the subshell. So the subshell source does actually work fine without any conflicts despite same VAR name already sourced... all that said, if anyone still can comment with non-source solutions, it would be great. I've searched for days and not many discussions on this topic, although I finally found one:
Can I export a variable to the environment from a bash script without sourcing it?