RydallCooper
RydallCooper

Reputation: 21174

Reading A File, And Extracting Each Line As It's Own Variable [Bash]

I've been stuck on this, and can't seem to find any good tips on getting this to work. Okay, so I have this file called "tweets" which has a couple hundred lines. I need to get each line as it's own variable.

Example:

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

Alright, so I need each line to be set as a variable. Thanks!

Upvotes: 2

Views: 91

Answers (2)

glenn jackman
glenn jackman

Reputation: 246807

In bash, use mapfile to read the file into an array:

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

Upvotes: 1

anubhava
anubhava

Reputation: 785156

You can use:

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

Upvotes: 1

Related Questions