0

I'm trying to get a list of specific variables and then the values of those variables?

#!/bin/bash

varA=1.3.5                # need this
varB=2.4.6                # need this
varC=3.6.9                # need this
ignoreA="Do not need"
ignoreB="Or this"

vars=`compgen -A variable | grep var`

for v in ${vars} ; do
  echo "${p} = $${p}"
done

I get the list of the variables, but can't work out how to get the value of each of those variables in the for-loop? I've tried a number of different combinations but can't wrap my head around what this should be:

  • ${$p}
  • {$$p}
  • $(${p})
2
  • See in particular BashFAQ #6. Usually, it's better practice to keep any arbitrary names in an associative array; but you can absolutely loop over names having a prefix without one as well, and both that FAQ and the linked duplicate describe how to do so. Commented Apr 9, 2020 at 22:47
  • for item in ${string}, btw, is generally a code smell and best avoided. If you want to track a list of things you can index into, iterate over, etc., use an array rather than a string for storing it. The caveats in BashPitfalls #1 apply; so does DontReadLinesWithFor. And in determining how to populate that array, be wary of BashPitfalls #50. Commented Apr 9, 2020 at 22:49

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.