0

I have a environment variable called NAME

export NAME="xyz"

and inside a file test.txt , I have a line

name=$NAME

I read the file and assigned the line to a variable y

y=`cat test.txt`

Now I am printing the variable y

echo $y

How can I get an output of name=xyz instead name=$NAME?

ilkkachu
  • 138,973
Jobs
  • 101
  • 3

1 Answers1

2

I think you need to implement eval for this particular usage. I'll explain using the command line:

  1. export $ABC (as you export $NAME)

    $ grep ABC .bashrc
    export ABC="xyz"
    
  2. set the variable in your txt file

    $ cat test.txt
    name=$ABC
    
  3. set the $y variable to the output of cat test.txt

    $ y=`cat test.txt`
    
  4. eval $y

    $ eval $y
    
  5. now name is set:

    $ echo $name
    xyz
    
WEBjuju
  • 506
  • thx @datUser for the edit – WEBjuju Jan 18 '18 at 16:21
  • Hi, What I need is, a string , name=xyz . envsubst is a solution , but I found some example where a file is given as argument to it. Can we give the variable $y as input to envsubst program – Jobs Jan 23 '18 at 10:39
  • You have not explained well why you need to do what it is you are doing. – WEBjuju Jan 23 '18 at 18:34
  • I have to read the line name=$NAME from a file and reaplce the env variable $NAME in it and would get a result of name=xyz (consider that value of NAME in env is xyz) as output.

    I am using this output to grep another string.

    – Jobs Feb 05 '18 at 05:21