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?

2
  • 1
    For environment variables, there's envsubst - see for example how do I get my code to use the value of the $HOME variable? Commented Nov 18, 2020 at 20:51
  • tears of joy I think this will work. As long as I have sourced all the appropriate files. oh boy Commented Nov 18, 2020 at 21:05

1 Answer 1

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
2
  • 1
    NB. This is not robust code: it will break if any directory name contains a space Commented Nov 18, 2020 at 23:51
  • ... I'd suggest envsubst < Directories.dat | while IFS= read -r line; do SomeCommand "$line"; done Commented Nov 19, 2020 at 1:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.