A handy trick in Bash is to split a stream to process it in more than one way without saving a temporary copy:
# Make 3 a copy of 1 (stdout)
exec 3>&1
result="$(command to generate stream \
| tee /dev/fd/3 \
| command to process stream)"
# Now work with $result
# The original stream also went to the console The actual case I worked with this morning was a very simple demonstration:
#!/bin/bash
exec 3>&1
echo -e "Bob is my friend.\nFred is my friend, too." \
| tee /dev/fd/3 \
| cut -f1 -d' ' >&2 Which simply prints:
Bob is my friend. Fred is my friend.
to stdout and to stderr it prints:
Bob Fred
1 comment:
Cool tip!! Thx
Post a Comment