问题
I have this:
echo "all done creating tables" >> ${SUMAN_DEBUG_LOG_PATH}
but that should only append to the file, not write to stdout. How can I write to stdout and append to a file in the same bash line?
回答1:
Something like this?
echo "all done creating tables" | tee -a "${SUMAN_DEBUG_LOG_PATH}"
回答2:
Use the tee
command
$ echo hi | tee -a foo.txt
hi
$ cat foo.txt
hi
回答3:
Normally tee is used, however a version using just bash:
#!/bin/bash
function mytee (){
fn=$1
shift
IFS= read -r LINE
printf '%s\n' "$LINE"
printf '%s\n' "$LINE" >> "$fn"
}
SUMAN_DEBUG_LOG_PATH=/tmp/abc
echo "all done creating tables" | mytee "${SUMAN_DEBUG_LOG_PATH}"
来源:https://stackoverflow.com/questions/44576935/echo-to-stdout-and-append-to-file