[Top][All Lists]

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

Re: bash "while do echo" can't function correctly

From: Geir Hauge
Subject: Re: bash "while do echo" can't function correctly
Date: Wed, 13 Apr 2016 08:10:15 +0200
User-agent: Mutt/1.5.23 (2014-03-12)

On Wed, Apr 13, 2016 at 10:01:23AM +0800, 何建军 wrote:
> Hi, I use a very simple statement "cat test.txt | while read line; do echo 
> $line; done",  test.txt is very small, no more than 100 lines. but the 
> execution of the statement  paused during process. test.txt is pasted on 
> http://paste.bradleygill.com/index.php?paste_id=1647399 
> address@hidden:/studio/desmond.he$ help | head -n 1 GNU bash, version 
> 4.3.11(1)-release (x86_64-pc-linux-gnu) address@hidden:/studio/desmond.he$ 
> cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=14.04 
> DISTRIB_CODENAME=trusty DISTRIB_DESCRIPTION="Ubuntu 14.04.3 LTS" Thanks for 
> help~

You didn't quote the expansion of the line variable, so the result of
the expansion is first split into words based on the characters in the
special IFS variable, then each of those words that contain glob
characters (like *, ? , [...]) will be replaced by matching filenames,
if any.

It's the last part that bites you here, since several of your lines
contain * characters. Worse if globstar is enabled, since there's also
** in there, so the "pause" you get is probably bash trying to recurse
through the entire filesystem, which may take a while.

The fix is easy. Just surround the expansion in double quotes, which
prevent word-splitting and pathname expansion.

    while read -r line; do echo "$line"; done < test.txt

though printf should be preferred over echo:

    while read -r line; do printf '%s\n' "$line"; done < test.txt

Geir Hauge

reply via email to

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