|
From: | Eric Blake |
Subject: | Re: conditional aliases are broken |
Date: | Thu, 18 Aug 2011 11:53:56 -0600 |
User-agent: | Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110621 Fedora/3.1.11-1.fc14 Lightning/1.0b3pre Mnenhy/0.8.3 Thunderbird/3.1.11 |
On 08/18/2011 11:38 AM, Stefano Lattarini wrote:
Hi Eric. On Thursday 18 August 2011, Eric Blake wrote:On 08/18/2011 08:44 AM, Eric Blake wrote:how do I write a function that would print the same as $ \ls | catUseless use of cat. This can be done with \ls -1.f(){ for a in "$@"; do echo "$a"; done; }
Actually, echo "$a" is not portable - if you have any file names beginning with - or containing \, then the results can be corrupted.
Or skip the loop altogether: f(){ printf %s\\n "%@"; }I think you've made a typo here; it should have been: f () { printf %s\\n "$@"; }
Yep, slip of one key when I typed (at least on my keyboard, % and $ are neighbors).
I guess that's what you meant, right? BTW, is this behaviour truly portable to other shells and/or printf utilities? POSIX seems to require it to portable, but you never know ...
It's portable, but not always fast (some shells lack printf(1) as a builtin, and end up spawning a process). And in the case of arbitrary file names, printf is always better than echo, since it handles \ and leading - correctly.
-- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org
[Prev in Thread] | Current Thread | [Next in Thread] |