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

  • SEARCH
  • Home
  • 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 9256897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:05:11+00:00 2026-06-18T12:05:11+00:00

This works well except it merges the standard error stream into the standard out

  • 0

This works well except it merges the standard error stream into the standard out stream of the sourcing script. Any suggestions on how to fix it?

#!/usr/bin/env bash

# Source this from a script to capture and `tee` standard error and standard 
# out to a log file. Calling script must source this script.  For Example:
#
# . /usr/bin/logy /var/log/project/$0.log
#
# The logging uses >(process substitution). Process substitution is supported 
# in shells like bash and zsh but is not supported in sh.

_LOG=${1?log file}
test $# -eq 1 || exit 1

mkdir -p "$(dirname "$_LOG")"

# Append stdout and stderr to log file
exec > >(
  echo -e "START\t$(date)" >> "$_LOG"
  tee -a "$_LOG"
  echo -e "END\t$(date)" >> "$_LOG"
) 2>&1

Here is an example:

. /usr/bin/logy $0.log
echo stdout
echo stderr >&2
exit 1

Run the script:

$ ./t
$ echo $?      # $? is the return value
1

Good, the return value 1 was preserved…

What was logged?

$ cat t.log
START    Thu, Feb 07, 2013  2:58:57 PM
stdout
stderr
END    Thu, Feb 07, 2013  2:58:57 PM

The idea is to make single log file and then use logrotate to maintain them.

Here is the issue. The standard out and error streams were merged. This outputs noting showing that the standard error stream went to standard out:

./t > /dev/null

This outputs both lines from the echo statements showing both went to standard out:

./t 2> /dev/null

Is there a good way to preserve the streams while preserving the order in the log file of the stdout/err statements? For that reason, I don’t think two exec statements are an option.

  • 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-06-18T12:05:12+00:00Added an answer on June 18, 2026 at 12:05 pm

    Good sir, this should work

    script_name.sh |& tee -a /var/log/script_name.sh.log
    

    To clarify part of the command

    |& was added to Bash 4 as an abbreviation for 2>&1 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this script below and it works well except that it is
I have an expect script to backup fortigate VDOM firewalls. This works well, except
I found this great class, all works well except... I spcify the output directory
I have tested this servlet and it works well, except in Google Chrome it
I have a php mail script that works well except for one minor fact.
Here is the code, everythin works well except the no application can perform this
I found this useful Internationalization code: http://pastebin.com/SyKmPYTX everything works well except I am unable
This code seems to work well, modifying the properties of my button, except the
Code like this works well. Intent configIntent = new Intent (context, WidgetConfigActivity.class); configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
I created a library bundle for commons-lang3 and this works well. I can build

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.