读取文件,并提取每一行,因为它是自己的变量[Bash]

时间:2014-02-05 21:03:55

标签: string bash file variables grep

我一直坚持这一点,似乎找不到任何关于让它发挥作用的好建议。 好的,所以我有一个名为“tweets”的文件,它有几百行。我需要让每一行都是它自己的变量。

示例:

   root@host:~$ cat tweets
   this is line one
   this is line two
   this is line three

好吧,所以我需要将每一行设置为变量。谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用:

while read line; do
    echo "$line"
done < tweets

答案 1 :(得分:1)

在bash中,使用mapfile将文件读入数组:

$ mapfile -t var < tweets
$ echo "${var[0]}"
   this is line one
$ echo "${var[2]}"
   this is line three
相关问题