[prog] Shell question...

John Clarke johnc+linuxchix at kirriwa.net
Fri Dec 10 09:14:42 EST 2004


On Thu, Dec 09, 2004 at 10:13:58 +0100, Riccarda Cassini wrote:

>   echo bla blah blurb | read FOO BAR BAZ

Try this instead:

    echo "bla blah blurb" | while read FOO BAR BAZ
    do 
        echo $FOO
        echo $BAR
        echo $BAZ
    done

> The Linux behaviour seems to make more sense to me, 'cos - as I
> eventually figured - the 'read' after the pipe is apparently being run
> in a subprocess, so the variables just go away when that process
> terminates.
> At least that's my current theory... Can't wait to hear yours ;-)

You're right.  I do remember seeing a proper explanation of this
somewhere, but I can't find it right now.  It goes something like
this:   you're piping output from one command (echo) to another (read),
so the shell runs one (or both?) of them in a child process.  Putting
the read in a while loop makes the variables available within the loop,
because the entire loop is run in the child process.

> P.S.: does anyone know how to find out which ksh versions I have?
> I tried 'ksh -v', 'ksh --version', 'ksh -h', but all to no avail.
> Also, there doesn't seem to be a respective shell variable I might

There is: 

    [johnc at dropbear ~]$ ksh
    $ echo $KSH_VERSION
    @(#)PD KSH v5.2.14 99/07/13.2

KSH_VERSION is listed in the man page :-)


Cheers,

John
-- 
I am going to go bang my head against the wall, for no other reason than 
it feel so good when I stop.
            -- Craig 'Stevo' Stephenson


More information about the Programming mailing list