Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Sorting files



On Fri, Aug 05, 2005 at 05:01:46AM +0200, Meino Christian Cramer wrote:
[...]
> > >     print -l **/*(.oL)
[...]
> > Should be:
> > 
> > print -rl -- **/*(.oL)
[...]
> This gave me no sorted output...sorry :O)


I was just pointing out that

print -l arbitrary list of file names

is not correct. print without -r is to print a text that is in
the form "text with \t \n... escape sequences" like in C string
constants. That's an old design error in shells inherited from
ksh to have that as the default behavior. For a correct way, see
perl for instance where \t, \n are expanded at the language
level (or by the double quotes if you like).

In perl,

$var = "\t"

assigns a <Tab> character to $var and print $var prints the
content of $var. ($var = '\t' assigns "\" and "t" to $var).

In shells,

var="\t"

assigns the "\" and "t" characters to $var and print "$var"
prints the expansion of the "\t" escape sequence, i.e. a <Tab>
character.

ksh93, bash and zsh have the cumbersome:

var=$'\t'

that does the same as perl's "\t", but print (and echo) are
still /broken/ and need the "-r" (and -n to prevent adding a
newline character) to print strings asis.


Without --, the list can be options or arguments, while you
definitely mean them to be arguments there.

Another annoying thing with print -l is that without arguments,
it still prints an empty line as if it had been given an empty
argument.

So that to print arguments one per line, you actually need:

correct_print-l() {
  (( $# == 0 )) || print -rl -- "$@"
}

or to be portable (POSIX):

correct_print_l() {
  [ "$#" -eq 0 ] || printf '%s\n' "$@"
}

-- 
Stephane



Messages sorted by: Reverse Date, Date, Thread, Author