1

Assume two files: file1 and file2. file1 is a short Bash script, that references file2 in order to obtain a text string. The text string includes a variable name ($VAR1), but the variable itself is assigned a value in file1.

$ cat file1
#!/bin/bash
VAR1="World"
CMDS=$(cat file2)
echo "$CMDS"

$ cat file2
Hello $VAR1 !

Under the above setup, the variable name is not recognized correctly during the execution of file1.

$ bash file1 
Hello $VAR1 !

What do I need to do so that the variable name is recognized correctly during the execution of file1?

Michael Gruenstaeudl
  • 1,609
  • 1
  • 17
  • 31

1 Answers1

0

As you do not execute file2, just cat its content, the variable in file2 only serves as a placeholder. You could pipe the output from cat to sed, like so:

CMDS=$(cat file2|sed -e "s/\$VAR1/$VAR1/")
T. Kuther
  • 610
  • 3
  • 7