Appending a variable which contains the tee command and log file name, not getting the expected result, since echo is printing the variable content.

Below is the file content, actual output and expected result.

Content of shell script:


log="2>&1 | tee -a report.txt"

echo ""
echo '***************-:START OF THE REPORT:-***********' $log

After running the script.

console op:
***************-:START OF THE REPORT:-*********** 2>&1 | tee -a report.txt


console op:
***************-:START OF THE REPORT:-***********

report.txt file content:
***************-:START OF THE REPORT:-***********

Also note that ,variable $log should contain both the tee command and file name, since I don't want to hard code the tee command at end of each echo command.


I'm assuming you would like to use this unusual way of tagging the actual pipe and tee onto the end of the log message because you don't want to have to pipe every echo?

Well, you can do it this way too:


log () {
    if [ -z "$1" ]; then
        tee -a "$logfile"
        printf '%s\n' "$@" | tee -a "$logfile"

log "some message"
log "some other message"
log "multi" "line" "output"

    cat <<LETTER
Dear Sir,

It has come to my attention, that a whole slew of things may be
collected and sent to the same destination, just by using a single
pipe. Just use { ... } | destination

Sincerely, $LOGNAME

    cat <<THE_PS
Here's the output of "ls -l":

    ls -l

    echo "LOL"

} | log

That is, wrap the awkward tee command in a simple shell function whose name is easy to type and just pipe the output to it.

In this example, the log function uses printf to output the data given on its command line, or switches to reading from standard input if these was no command line arguments.

You could even use

./your_original_script_without_special_logging 2>&1 | log
  • How about commands like "mount" which gives multiple line output, which I want to print on console as well as log the output in a log file. – Prashant BJ Sep 9 '17 at 17:21
  • @PrashantBJ See my example. mount | log. – Kusalananda Sep 9 '17 at 17:24

You should actually use a function masking the command echo for this:

echo ()
    builtin echo "$@" | tee -a report.txt

Then you can do:

echo ""
echo '***************-:START OF THE REPORT:-***********'

Without having to hardcode tee or anything else at the end of echo commands.

  • 2
    I'd recommend to name the function as log or similar, so you can still use the usual echo command when you don't want its output to be logged. – nxnev Sep 9 '17 at 13:00
  • @nxnev there's always builtin echo if you want to override the function. – muru Sep 9 '17 at 13:28

They way your are using the

echo '***************-:START OF THE REPORT:-***********' $log

line just echoes the content of $log to the command line, not acting as if you would enter that line in a console.

To make the line act as you expect you can or use eval.

eval echo '***************-:START OF THE REPORT:-***********' $log
  • Thanks Thomas ,your eval command input works fine. Tried your input "$(echo $log)", is not working and its just printing the variable content. – Prashant BJ Sep 9 '17 at 13:33
  • Sorry, my bad. Updated my answer. But to be honest, I would choose the answer from Kusalananda in your case. – Thomas Sep 9 '17 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.