Alexander Mills
Alexander Mills

Reputation: 100320

echo to stdout and append to file

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?

Upvotes: 10

Views: 11325

Answers (3)

mauro
mauro

Reputation: 5950

Something like this?

echo "all done creating tables" | tee -a  "${SUMAN_DEBUG_LOG_PATH}"

Upvotes: 17

CowboyTim
CowboyTim

Reputation: 57

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

Upvotes: 1

Dan Schmidt
Dan Schmidt

Reputation: 61

Use the tee command

$ echo hi | tee -a foo.txt
hi
$ cat foo.txt
hi

Upvotes: 4

Related Questions