Search code examples
shellcsh

file-list with env variable


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?


Solution

  • 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?