0

So I have a file "Directories.dat" that contains a list of directories for a script to read that runs on multiple machines. Due to this, the list of directories in the file is often defined with variables such as

#Directories.Dat
/home/$USER
$WORKDIR
$APPDIR
...etc 

However, when the script runs through the file Directories.dat , $line ends up being set to /home/$USER rather than /home/myuser

#!/bin/bash
#myscript.sh
for line in $(cat Directories.dat) 
do 
SomeCommand $line
done

Should I not use cat? Is eval the only suitable function for doing this? example SomeCommand $(eval echo $line) #DangerZone or are there other methods to read the lines within the Directories.dat file and show the variables as their stored values?

roaima
  • 107,089
  • 14
  • 139
  • 261
link
  • 1
  • 1
  • 1
    For *environment* variables, there's `envsubst` - see for example [how do I get my code to use the value of the $HOME variable?](https://unix.stackexchange.com/questions/554333/how-do-i-get-my-code-to-use-the-value-of-the-home-variable) – steeldriver Nov 18 '20 at 20:51
  • *tears of joy* I think this will work. As long as I have sourced all the appropriate files. oh boy – link Nov 18 '20 at 21:05
  • yep, worked like a charm. Holy hell man thank you – link Nov 18 '20 at 21:08

1 Answers1

0

Moved from the question

#!/bin/bash
source bashrc.sh mysources.sh
#myscript.sh
for line in $(cat Directories.dat) 
do 
SomeCommand $(echo $line | envsubst)
done
roaima
  • 107,089
  • 14
  • 139
  • 261