Reputation: 751
In my bash script I have a function that:
expect
script spawning an SSH connection to a remote deviceecho
's back a string to that effect (as a return value)When I run the script from the terminal and the bash script reaches the statement of:
myExpectFunc
[further code...]
why is it that I can see the console output of the expect
script, but if I do the following:
retVal=$(myExpectFunc)
[further code...]
there will be no console output? It is like it is suppressed until retVal
has been assigned a value.
I'd like to keep local
variables to functions and return the values of these variables as my return value for me to be able to case
on. Of course if I don't declare local
variables to the function the variable will be global and I can simply case
on the global variable. But I'd rather not do this. So is there a way to be able to maintain the console output and assign the return value to retval
?
Upvotes: 1
Views: 160
Reputation: 113864
So is there a way to be able to maintain the console output and assign the return value to retval?
Yes. Use tee
:
retVal=$(myExpectFunc | tee /dev/tty)
All of the standard output from myExpectFunc
is sent to the standard input of tee
. tee
copies that to both to the file /dev/tty
(which is your terminal) and to its own standard out (which is then captured by retVal
).
Upvotes: 1