0

I'm trying to get a value of the variable by double substituting inside the string as follows

i=1
df1 ='date_col'

X='value of the string is $df$i'

But getting invalid substitution error

I need the output as follows

echo $X 
value of the string is date_col
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Harish H B
  • 13
  • 4
  • What shell are you using? – Shawn Nov 07 '22 at 17:32
  • What you want is called "indirect expansion". There are better ways to do it in extended shells such as bash, ksh, or zsh; the only approaches available with `/bin/sh` [have security issues](https://mywiki.wooledge.org/BashFAQ/048). – Charles Duffy Nov 07 '22 at 18:33
  • For an extensive discussion, see [BashFAQ #6](https://mywiki.wooledge.org/BashFAQ/006). – Charles Duffy Nov 07 '22 at 18:35
  • (also, if you're using an extended shell, you have arrays; `declare -a df=( [1]=date_col )` and then you can refer to `${df[1]}`; if the keys can be arbitrary strings instead of just numeric, then in bash one would use `declare -A` instead) – Charles Duffy Nov 07 '22 at 18:36
  • I'm using bash shell – Harish H B Nov 08 '22 at 01:47

0 Answers0