具有env变量的文件列表

时间:2018-07-11 12:16:43

标签: shell csh

在linux中,我有一个名为 file_list.txt 的文件列表,该列表中的路径还包括一个env变量(对于此示例,列出的文件该文件列表中的 $ 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

那是为什么?如何在文件列表中列出的文件上运行操作(例如ls,less,vim等)?

1 个答案:

答案 0 :(得分:1)

如果要扩展变量,则需要类似以下内容的

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

在这种情况下,命令是从字符串中读取的,因此可以扩展变量(如果有)。

$ eval "ls `cat file_list.txt`"

以防万一,还要检查以下问题:Why should eval be avoided in Bash, and what should I use instead?