0

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"

sagar
  • 190
  • 3
  • 12

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
  • 257,635
  • 38
  • 339
  • 400
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,340
  • 2
  • 29
  • 45