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

Re: Problem mit read (bash-builtin)



Am Freitag, 29. Juni 2012 schrieb Ulf Volmer:
> On Fri, Jun 29, 2012 at 02:53:58PM +0200, Martin Eberhard Schauer wrote:
> > >Ich habe ein Programm, dass genau ein Wort ausgibt, z.B. echo
> > >"hallo". Den output möchte ich dann in eine shell-variable
> > >einlesen. Mein Versuch: echo "hallo" | read x
> > >danach ist x aber unbelegt und echo $x liefert eine Leerzeile.
> > >Was mache ich da falsch?
> > 
> > Hallo Simon,
> > 
> > auf die schnelle kann ich nicht sagen, was bei der Pipeline in die
> > Hose geht. Versuch es mal mit
> 
> Das Problem ist, dass durch die | eine neue Shell gestartet wird und
> die Variable x dann nur in der neuen Shell verfügbar ist.
> 
> Das sieht man z.B. mit
> 
> echo hallo | (read x;echo $x)
> hallo

Bei der Z-Shell offenbar nicht:

martin@merkaba:~> echo "hallo" | read x
martin@merkaba:~> echo $x
hallo

martin@merkaba:~> bash                 
martin@merkaba:~ ->
martin@merkaba:~ -> echo "hallo" | read x
martin@merkaba:~ ->
martin@merkaba:~ -> echo $x

 
> >    var=`$dein_Programm`
> 
> Exakt, so funktioniert es.

Wobei die Backticks deprecated sind – warum auch immer hab ich nicht mehr 
im Kopf.

x=$(echo hallo)

tuts aber auch. Oder in dem einfachen Fall besser:

x="hallo"

;)

Ciao,
-- 
Martin 'Helios' Steigerwald - http://www.Lichtvoll.de
GPG: 03B0 0D6C 0040 0710 4AFA  B82F 991B EAAC A599 84C7


Reply to: