0

How to expand only bash $variable at any certain point of anything (time, condition, etc) ? as this works :

 i=1
  u="sum = $i + \$o"
  o=two; n=$(eval echo $u)

to equalize to n='sum = 1 + two'

the truth, $(eval echo $) won't work for complex long expression The real case the u above is some find arguments as, e.g., the output of set -x:

C='( -ipath /home/olive/build/CMakeFiles/* (  ( -type d ( -regex .*\s.* -printf '\''%p/'\''\n -o -printf %p/\n ) -o ( -regex .*\s.* -printf '\''%p'\''\n -o -printf %p\n ) ) ) -o -ipath /home/olive/build/CMakeFiles -type f ( -regex .*\s.* -printf '\''%p'\''\n -o -printf %p\n ) -o -iregex .{17}/.+/CMakeFiles$RP ( -type d ( -regex .*\s.* -printf '\''%p/'\''\n -o -printf %p/\n ) -o ( -regex .*\s.* -printf '\''%p'\''\n -o -printf %p\n ) ) )'

So $RP there is big need to be expanded after once expansion (like $o on sample)

So how to expand only a bash variable at any certain arbitrary point without touch any else expression in which the var. sit

  • 1
    Use an array instead, like `C=(foo '' bar); C[1]=x; find "${C[@]}"; C[1]=y; find "${C[@]}"`. There is no reason to use eval and a scalar variable here. – oguz ismail Mar 14 '21 at 11:53

1 Answers1

0

If you are asking something like the below shows (indirect variable lookup) - then use ${!variablename} to do an indirect lookup of the variable whose name is in variablename.

e.g.

#!/bin/bash

v1=23
v2=v1

echo "Lookup ${!v2} should be 23"

v1=49

echo "Lookup ${!v2} should be 49"

Gives this output ..

Lookup 23 should be 23
Lookup 49 should be 49
Mr R
  • 754
  • 7
  • 19