0

I would like to split a text file with IFS my text file looks like :

name1
name2
name3
name4

I want to read this file and get name by name here is my code :

names=$(</text.txt)
IFS='\n' read -a -r names_list <<< "$names"

for name in "${names_list[@]}"
do
    echo "$name"
done

it's always showing the first name and not the others, any solution ?

Stranger B.
  • 9,004
  • 21
  • 71
  • 108

1 Answers1

1

I am unsure about the output you are looking for but to go through a file using a loop, here is the syntax:

sylvainkalache@holbertonschool$ cat text.txt
name1
name2
name3
name4
sylvainkalache@holbertonschool$ while read name
> do
> echo "$name"
> done < /tmp/file
name1
name2
name3
name4
sylvainkalache@holbertonschool$

You actually do not need to change the IFS variable.

Sylvain Kalache
  • 381
  • 2
  • 8