If you have a string with a delimiter, let's say a ,
character, you could use IFS
just like that:
text=some,comma,separated,text
IFS="," read -ra ADDR <<< "$text"
for i in ${ADDR[@]}
do
echo $i
done
Each word will be printed in a new line. But if you grab the result of command like ls
and then try to split it on the \n
you don't get to the same result:
results=$(ls -la)
IFS="\n" read -ra ADDR <<< "$results"
for i in ${ADDR[@]}
do
echo $i
done
It only prints 2 lines, and they are not even the file entries. It is
total
36
The first line of the ls
command output.
Can someone give a little help? If it is not the correct way, how is that?
ls
output or writing shell loops to manipulate text. – LucileluciliaIFS="\n"
adds the two characters\
andn
toIFS
, not a single newline character. You would wantIFS=$'\n'
, but this is not a good way to iterate over the files in a directory as Ed Morton points out. – Subeditor