Environment variable in variable

I have an environment variable called $TESTthat refers to a directory in my bash script I have a variable with a name $VARTESTthat$TEST/dir/file

now I want the grep file specified $VARTEST, so I'm trying to do:
grep somestring $VARTESTbut it does not translate the $TESTdirectory into it

I tried different combinations {}, ""and ''but without success

+3
source share
2 answers

I think you want

eval grep somestring "$VARTEST"

or even

VARTEST_EVALUATED=$(eval echo $VARTEST)
grep "$SOMESTRING" "$VARTEST_EVALUATED"

but remember (as others have already said): If possible, use

VARTEST="$TEST/foo/bar"

instead

VARTEST='$TEST/foo/bar'

use the second one only if you really need a lazy rating ...

, , $VARTEST .

+5

-? . . :

#!/bin/sh

TEST="/etc"
VARTEST="$TEST/passwd"
grep "$LOGNAME" "$VARTEST"
+1

Source: https://habr.com/ru/post/1704846/


All Articles