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

Re: cat as a builtin command

On 29 August 2014 15:03, Peter Stephenson <p.stephenson@xxxxxxxxxxx> wrote:
> On Fri, 29 Aug 2014 09:40:29 +0800
> Izumi Natsuka <izumi.natsuka@xxxxxxxxxxx> wrote:
>> Hello, I'm going to write a shell function that provides a basic
>> functionality(print the content of a file or stdin) of cat[0], in
>> order to avoid forking too many process when I call it in a
>> loop[1]. And I have put the following in my zshrc:
> If it's just printing the contents of a file, investigate zsh/mapfile.
> This turns off multibyte mode to make it easier to do binary files ---
> there should be no disadvantage here as you just want a byte stream.
> mycat() {
>   emulate -L zsh
>   unsetopt multibyte
>   zmodload zsh/mapfile
>   print -r $mapfile[$1]
> }
> This only works for files in the file system, not file descriptors,
> pipes etc.  For those you can often optimise out the cat entirely.
> pws

mycat() { for i { print -r - "$(< $i)" } }
(also only works for files)

Mikael Magnusson

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