The problem#
$ ls > file
doesn't do what you expect:
$ touch foo
$ touch bar
$ ls > filelist
$ cat filelist
bar
filelist
foo
You probably didn't expect, or want, filelist
to be listed in filelist
.
The solution#
$ filelist=$(ls); echo "$filelist" >filelist