Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 962791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:33:56+00:00 2026-05-16T01:33:56+00:00

I know how to redirect stdout to a file: exec > foo.log echo test

  • 0

I know how to redirect stdout to a file:

exec > foo.log
echo test

this will put the ‘test’ into the foo.log file.

Now I want to redirect the output into the log file AND keep it on stdout

i.e. it can be done trivially from outside the script:

script | tee foo.log

but I want to do declare it within the script itself

I tried

exec | tee foo.log

but it didn’t work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:33:57+00:00Added an answer on May 16, 2026 at 1:33 am
    #!/usr/bin/env bash
    
    # Redirect stdout ( > ) into a named pipe ( >() ) running "tee"
    exec > >(tee -i logfile.txt)
    
    # Without this, only stdout would be captured - i.e. your
    # log file would not contain any error messages.
    # SEE (and upvote) the answer by Adam Spiers, which keeps STDERR
    # as a separate stream - I did not want to steal from him by simply
    # adding his answer to mine.
    exec 2>&1
    
    echo "foo"
    echo "bar" >&2
    

    Note that this is bash, not sh. If you invoke the script with sh myscript.sh, you will get an error along the lines of syntax error near unexpected token '>'.

    If you are working with signal traps, you might want to use the tee -i option to avoid disruption of the output if a signal occurs. (Thanks to JamesThomasMoon1979 for the comment.)


    Tools that change their output depending on whether they write to a pipe or a terminal (ls using colors and columnized output, for example) will detect the above construct as meaning that they output to a pipe.

    There are options to enforce the colorizing / columnizing (e.g. ls -C --color=always). Note that this will result in the color codes being written to the logfile as well, making it less readable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that there're stdout/in/err for a program and I want to redirect a
I want to know the difference between : $this->forward(module, action); And $this->redirect(module/action); My first
I know how to redirect the stdout to a file, but I have no
In bash you can do: echo test >&1 (redirect to stdout, although it's already
I want to know whether 301 redirect always preserve referrer. I make a page
I'm writing a function, which, given an argument, will either redirect the stdout to
Possible Duplicate: Redirect stdin and stdout in Java I know how to execute shell
How do I redirect stdout to an arbitrary file in Python? When a long-running
Most of us know that to redirect STDERR to STDOUT we do 2>&1 We
This one's been bugging me for a while now. Is it possible to redirect

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.