It looks like you're trying to do
while read line
do
...
done < <(ls -ltr | awk '{print $9}')
but why? Piping into while read line
(Archemar's answer)
is clearer and more portable.
OK, to make my answer more complete:
as pointed out by Zoltán Böszörményi,
ls -ltr | awk '{print $9}' | while read line
do
...
done
in bash (and some other shells), runs the body of the while
in a subshell,
so state changes to the shell state (e.g., shell variables)
will not persist beyond the loop.
This is discussed more fully in the following places:
The < <(…)
code at the top of this answer
is one way to avoid that problem.
But another thing
You’re doing ls -ltr | awk '{print $9}'
.
That looks like a kludge to list files in order of modification date/time
and then extract the file names.
A couple of issues:
- This fails for filenames that contain whitespace.
- You’re making extra work for yourself
by telling
ls
to generate long listing information
(file mode, owner, size, etc.)
and then stripping it out,
when you don’t need to generate it in the first place.
You can address both of these issues
by leaving off the l
option to ls
, and leaving off the awk
:
while read line
do
...
done < <(ls -tr)
This may still have problems with filenames
that begin or end with whitespace.
See also Why you shouldn’t parse the output of ls(1).