Skip to main content
2 of 3
deleted 4 characters in body
muru
  • 77.9k
  • 16
  • 212
  • 318

You can redirect stderr/stdout to a process substitution that adds the prefix of choice. For example, this script:

#! /bin/bash
exec > >(sed 's/^/foo: /')
exec 2> >(sed 's/^/foo: (stderr) /' >&2)
echo foo
echo bar >&2
date

Produces this output:

foo: foo
foo: (stderr) bar
foo: Fri Apr 27 20:04:34 IST 2018

The first two lines redirect stdout and stderr respectively to sed commands that add foo: and foo: (stderr) to the input.

muru
  • 77.9k
  • 16
  • 212
  • 318