0

In linux, I have a file-list named file_list.txt, where the paths in that list include also an env variable (for this example, the file listed in that file-list is $HOME/myfile).

> cat file_list.txt
$HOME/myfile

> ls `cat file_list.txt`
ls: $HOME/myfile: No such file or directory

> ls $HOME/myfile
/home/user/myfile

Why is that? How can I run operations (such as ls, less, vim etc) on the files listed in the file-list?

1 Answers1

1

If you want to expand the variables you will need something like:

$ sh -c "ls `cat file_list.txt`"

In this case, the commands are read from string and therefore extending variables if any.

$ eval "ls `cat file_list.txt`"

Just in case check also this question: Why should eval be avoided in Bash, and what should I use instead?

nbari
  • 25,603
  • 10
  • 76
  • 131
  • Thank you for your response. When trying it, however, I get the following error (same response to either of the options you gave): `Illegal variable name` – Hagai Naveh Jul 11 '18 at 13:11