views:

64

answers:

1

I have a bash function that produces some output:

function scan {
  echo "output"
}

How can I assign this output to a variable?

ie. VAR=scan (of course this doesn't work - it makes VAR equal the string "scan")

+6  A: 
VAR=$(scan)

Exactly the same way as for programs.

Robert Obryk
Can this be done preserving the newlines?
Brent
I discovered that newlines were being stripped when I did "echo $VAR". If instead I quoted $VAR, it preserved the newlines.
Brent
That's not 100% right. Command substitution always strips trailing newlines.
TheBonsai