Take the 2-minute tour ×

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")

share|improve this question

1 Answer 1

up vote 23 down vote accepted

Exactly the same way as for programs.

share|improve this answer
Can this be done preserving the newlines? –  Brent Nov 27 '09 at 17:46
I discovered that newlines were being stripped when I did "echo $VAR". If instead I quoted $VAR, it preserved the newlines. –  Brent Nov 27 '09 at 17:58
That's not 100% right. Command substitution always strips trailing newlines. –  TheBonsai Nov 28 '09 at 17:36

Your Answer


