How would I go about getting the values of local and global variables in Bash, skipping any functions and environment variables?
declare -p gives me the whole bunch. But it's not clear whether I can rely on the output format of declare -p. From the help declare output it is also not obvious whether whether declare -p may include functions or not. Experimentation suggests it doesn't.
Example: there are variables whose output will be similar to:
declare -- VARNAME="Variable value"
... and others where the options to declare are given instead of --, e.g.:
declare -ir UID="197609"
Now, if I knew that I can rely on this format for the output of declare, I could simply use a while loop similar to this one to limit the output to read-only integer variables:
while read -r decl opts keyval; do
if [[ "$opts" == *ir* ]]; then # could obviously also be case/esac
echo "$keyval"
fi
done < <(declare -p)
... and thereby weed out the types of variables I am not interested in.
Question: But can I rely on this to be the case? If not, what other concise method exists to filter the variables by some given criteria?
NB: I am interested in Bash >=5.0 and I am currently using 5.2.15 on my machine.
declare -p -ilists integer variables,declare -p -rlists read-only variables, butdeclare -p -riis an "OR" (union) operation between these two sets rather than a "AND" (intersection), so that would only be partially helpful.