1

How to redirect command output to variable and console same time.

Example:

 var=`ls -l`

I want to print output in console at the same time and not later "echo $var"

GoneCase123
  • 388
  • 4
  • 15

2 Answers2

2
var=$(ls -l | tee /dev/stderr)

Note that this is assumed to just be an example -- you should never use ls in scripts, except where the output will only be consumed by human readers.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
1

You're looking for the tee command.

DESCRIPTION

   Copy standard input to each FILE, and also to standard output.
Community
  • 1
  • 1
Will Bickford
  • 5,381
  • 2
  • 30
  • 45