Skip to main content
added 473 characters in body
Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k

As Jeff pointed out, it will be easier to create your array sorted, as in:

set -A files $(ls -trtrd -- ~/a ~/b ~/c)

Here splitting the output of ls on $IFS character (by default containing SPC, TAB, LF and NUL). To split on LF only (to be able to work with filenames than contain SPC or TAB characters (but not LF obviously)), you can set IFS=$'\n' or use the f parameter expansion flag:

files=(${(f)"$(ls -trd -- $files)"})

(here also using zsh-style array=(...) syntax instead of ksh88-style set -A array ... since that syntax is already zsh-specific anyway).

As Jeff pointed out, it will be easier to create your array sorted, as in:

set -A files $(ls -tr ~/a ~/b ~/c)

As Jeff pointed out, it will be easier to create your array sorted, as in:

set -A files $(ls -trd -- ~/a ~/b ~/c)

Here splitting the output of ls on $IFS character (by default containing SPC, TAB, LF and NUL). To split on LF only (to be able to work with filenames than contain SPC or TAB characters (but not LF obviously)), you can set IFS=$'\n' or use the f parameter expansion flag:

files=(${(f)"$(ls -trd -- $files)"})

(here also using zsh-style array=(...) syntax instead of ksh88-style set -A array ... since that syntax is already zsh-specific anyway).

added 7 characters in body
Source Link

As Jeff pointed out, it will be easier to create your array sorted, as in:

files=$set -A files $(ls -tr ~/a ~/b ~/c)

As Jeff pointed out, it will be easier to create your array sorted, as in:

files=$(ls -tr ~/a ~/b ~/c)

As Jeff pointed out, it will be easier to create your array sorted, as in:

set -A files $(ls -tr ~/a ~/b ~/c)
Source Link

As Jeff pointed out, it will be easier to create your array sorted, as in:

files=$(ls -tr ~/a ~/b ~/c)