Fog Creek Software
Discussion Board

Quickie Bourne shell question

How do I catch the output of a program and stick it into a variable, inside a Bourne shell script?

Thursday, June 24, 2004

Without creating a temporary file.

Thursday, June 24, 2004

use backquotes:
set results =  `ls /etc`

then access results with $results

Andrew Hurst
Thursday, June 24, 2004

Is that what RP is really asking for? He said catch which some would construe as get and hold. What that does is regenerate them each time without saving.

The $results is aliased to the command in the backquotes, so it is not saving the results from that one program run and stashing them away, rather it is rerunning the program every time $results comes up.

There is the possibility that the results will have changed when run twice within the same shell process.

Thursday, June 24, 2004

No it certainly isn't...

$ export whattime=`date`
$ echo $whattime
Thu Jun 24 18:24:34 CEST 2004
$ sleep 10 ; echo $whattime
Thu Jun 24 18:24:34 CEST 2004

Also, I prefer the $() notation but that doesn't matter.

Björn Roth
Thursday, June 24, 2004

set x=`ls` works in c shell but in bourne or ksh. use x=`ls` instead.

set is a bourne shell command but does not assign values to a variable.

Also bourne and ksh don't recognize spaces around the =.

so x =`ls` does not work.

Thursday, June 24, 2004

*  Recent Topics

*  Fog Creek Home