-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction-return.sh
More file actions
executable file
·36 lines (25 loc) · 974 Bytes
/
Copy pathfunction-return.sh
File metadata and controls
executable file
·36 lines (25 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash
set -e
# Duplicate the original stdout fd so that the function can use it
exec {STDOUT}>&1
function func() {
# At this point, the function's stdout fd (1) is redirected to be captured by the function caller
# Move the return fd (from 1 to $RETURN)
exec {RETURN}>&1-
# Restore the original stdout (duplicates $STDOUT to 1)
exec 1>&$STDOUT
echo 'This should go to standard output'
echo 'This should go to error output' >&2
echo 'This should be returned by the function' >&$RETURN
# Close the return fd ($RETURN)
#exec {RETURN}>&- # Not really needed!
}
function func-simple() {
echo 'This should go to standard output [simple]'
echo 'This should go to error output [simple]' >&2
echo 'This should be returned by the function [simple]' >&$RETURN
} {RETURN}>&1- 1>&$STDOUT
echo "The function returns \"$(func)\""
echo "The function returns \"$(func-simple)\""
# Close the duplication ($STDOUT) of the original stdout fd
exec {STDOUT}>&-