Skip to content

Instantly share code, notes, and snippets.

@reduardo7
Last active April 29, 2023 20:56
Show Gist options
  • Save reduardo7/5d8443130495d3ad0e630eb344a37707 to your computer and use it in GitHub Desktop.
Save reduardo7/5d8443130495d3ad0e630eb344a37707 to your computer and use it in GitHub Desktop.
Output prefix with timestamp
#!/usr/bin/env bash
{
echo foo 1
sleep 1
echo bar 1 >&2
echo foo 2
sleep 1
echo bar 2 >&2
} \
2> >(sed "s/^/$(date '+%Y-%m-%d %H:%M:%S') [ERR] /" >&2) \
> >(sed "s/^/$(date '+%Y-%m-%d %H:%M:%S') [INF] /")
#!/usr/bin/env bash
output-prefix() {
"$@" \
2> >(sed "s/^/$(date '+%Y-%m-%d %H:%M:%S') [ERR]${prefix} /" >&2) \
> >(sed "s/^/$(date '+%Y-%m-%d %H:%M:%S') [INF]${prefix} /")
}
output-prefix ./loop.sh foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment