I am trying to process some files in directory using foreach loop for ls output:  IFS=$'\n'for i in $(ls -1Atu); do    echo $idoneAt first time i thought it works, but when i created file with name like * or filename*, shell add additional iterations, because * interpreted as wild char.But if I escape ls output like for i in $(ls -1Atu);, I'll get a single iteration, and $i will contain the output from ls entirely.How can we solve this problem? I should note that any variants with ls -1Atu preferable, as I need, what would the resulting files were sorted by atime.Thank you
Foreach loop for ls output
shell;ls;filenames;wildcards;for
Background reading: Why does my shell script choke on whitespace or other special characters?, Why you shouldn't parse the output of lsSetting IFS to a newline means that only newlines, and not spaces and tabs, will be treated as separators during the expansion of the command substitution. Your method will not support file names that contain newlines; this is a fundamental limitation of ls.Furthermore, and this is what you ran against, setting IFS has no effect on the other thing that happens on the expansion of a command substitution, which is globbing (wildcard matching). You can turn off globbing, and then your script will work as long as file names don't contain newlines.IFS=''set -- *$IFS*if [ -e $1 ]; then  echo >&2 There are file names with newlines. I cannot cope with this.  exit 2fiset -ffor i in $(ls -1Atu); do    printf '%s\n' $idoneset +funset IFSThe easy, reliable way to enumerate files by date is to use something other than a pure Bourne-style shell, such as zsh which has glob qualifiers to modify the way wildcard matches are sorted.#!/bin/zshfiles_by_access_time=(*(Doa))  You can work around it with some ls implementations, but not if you need portability.  