bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why `echo -n hello | while read v; do echo $v; done' prints nothing?


From: DennisW
Subject: Re: Why `echo -n hello | while read v; do echo $v; done' prints nothing?
Date: Mon, 6 Dec 2010 15:17:28 -0800 (PST)
User-agent: G2/1.0

On Dec 6, 2:39 pm, Stephane CHAZELAS <stephane_chaze...@yahoo.fr>
wrote:
> 2010-12-2, 19:04(+08), Clark J. Wang:
>
> > Following command also prints nothing, confused :(
>
> > for ((i = 0; i < 10; ++i)); do echo -n " $i"; done | while read v; do echo
> > $v; done
>
> Basically, if you want the equivalent of "cat" (if we let aside
> null bytes and performance), you'd need:
>
> while IFS= read -r v; do
>   printf '%s\n' "$v"
> done
> [ -z "$v" ] || printf %s "$v"
>
> --
> Stephane

If you don't mind doing it a character at a time, it can handle nulls,
too:

bashcat () {
    while IFS= read -r -d '' -n 1 c
    do
        [[ $c ]] &&
            printf '%s' "$c" ||
            printf '\0'
    done
}


reply via email to

[Prev in Thread] Current Thread [Next in Thread]