Add prefix to output #57

Closed
opened 2025-01-14 10:43:40 +01:00 by Benjamin_Loison · 6 comments
Owner
test.sh:
#!/bin/bash

prefixFunction() {
    sed "s/^/`date`: /"
}

#exec > >(trap "" INT TERM; prefixFunction)
#exec 2> >(trap "" INT TERM; prefixFunction >&2)
echo 'to stdout'
echo 'to stderr' >&2
./test.sh
Output:
to stdout
to stderr

Uncommenting comments in test.sh:

./test.sh
Output:
Tue Jan 14 09:42:38 AM CET 2025: to stdout
Tue Jan 14 09:42:38 AM CET 2025: to stderr

Source: the Unix Stack Exchange answer 440439

<details> <summary><code>test.sh</code>:</summary> ```bash #!/bin/bash prefixFunction() { sed "s/^/`date`: /" } #exec > >(trap "" INT TERM; prefixFunction) #exec 2> >(trap "" INT TERM; prefixFunction >&2) echo 'to stdout' echo 'to stderr' >&2 ``` </details> ```bash ./test.sh ``` <details> <summary>Output:</summary> ``` to stdout to stderr ``` </details> Uncommenting comments in `test.sh`: ```bash ./test.sh ``` <details> <summary>Output:</summary> ``` Tue Jan 14 09:42:38 AM CET 2025: to stdout Tue Jan 14 09:42:38 AM CET 2025: to stderr ``` </details> Source: [the Unix Stack Exchange answer 440439](https://unix.stackexchange.com/a/440439)
Author
Owner
Would help [Benjamin_Loison/openssh/issues/38#note_571962](https://salsa.debian.org/Benjamin_Loison/openssh/-/issues/38#note_571962).
Author
Owner
It helps [Benjamin_Loison/etesync-dav/issues/10#issuecomment-8659671](https://codeberg.org/Benjamin_Loison/etesync-dav/issues/10#issuecomment-8659671).
Author
Owner

exec > >(trap "" INT TERM; :) seems to reset the behavior, source: the Stack Overflow question 12404661, source: DuckDuckGo search Bash no op function.

Related to Benjamin-Loison/android/issues/59#issuecomment-3684308682.

`exec > >(trap "" INT TERM; :)` seems to reset the behavior, source: [the Stack Overflow question 12404661](https://stackoverflow.com/q/12404661), source: DuckDuckGo search *Bash no op function*. Related to [Benjamin-Loison/android/issues/59#issuecomment-3684308682](https://github.com/Benjamin-Loison/android/issues/59#issuecomment-3684308682).
Author
Owner
Bash script:
prefixFunction() {
    sed "s/^/`date`: /"
}

exec > >(trap "" INT TERM; prefixFunction)
sleep 1
date
Mon 23 Mar 00:08:21 CET 2026: Mon 23 Mar 00:08:22 CET 2026

shows that prefixFunction seems compiled once and for all.

DuckDuckGo search Linux prefix date to stdout.

<details> <summary>Bash script:</summary> ```bash prefixFunction() { sed "s/^/`date`: /" } exec > >(trap "" INT TERM; prefixFunction) sleep 1 date ``` </details> ``` Mon 23 Mar 00:08:21 CET 2026: Mon 23 Mar 00:08:22 CET 2026 ``` shows that `prefixFunction` seems compiled once and for all. DuckDuckGo search *Linux prefix date to stdout*.
Author
Owner

The Unix Stack Exchange answer 26797:

whoami | ts
Mar 23 00:09:32 benjamin_loison
[The Unix Stack Exchange answer 26797](https://unix.stackexchange.com/a/26797): ```bash whoami | ts ``` ``` Mar 23 00:09:32 benjamin_loison ```
Author
Owner

For within the second precision:

ts --help
Output:
Unknown option: help
usage: ts [-r] [-i | -s] [-m] [format]
man ts | cat
Output:
man: can't set the locale; make sure $LC_* and $LANG are correct
TS(1)                                                                                                                                TS(1)

NAME
       ts - timestamp input

SYNOPSIS
       ts [-r] [-i | -s] [-m] [format]

DESCRIPTION
       ts adds a timestamp to the beginning of each line of input.

       The optional format parameter controls how the timestamp is formatted, as used by strftime(3). The default format is "%b %d
       %H:%M:%S". In addition to the regular strftime conversion specifications, "%.S" and "%.s" and "%.T" are like "%S" and "%s" and
       "%T", but provide subsecond resolution (ie, "30.00001" and "1301682593.00001" and "1:15:30.00001").

       If the -r switch is passed, it instead converts existing timestamps in the input to relative times, such as "15m5s ago". Many
       common timestamp formats are supported. Note that the Time::Duration and Date::Parse perl modules are required for this mode to
       work. Currently, converting localized dates is not supported.

       If both -r and a format is passed, the existing timestamps are converted to the specified format.

       If the -i or -s switch is passed, ts reports incremental timestamps instead of absolute ones. The default format changes to
       "%H:%M:%S", and "%.S" and "%.s" can be used as well. In case of -i, every timestamp will be the time elapsed since the last
       timestamp. In case of -s, the time elapsed since start of the program is used.

       The -m switch makes the system's monotonic clock be used.

ENVIRONMENT
       The standard TZ environment variable controls what time zone dates are assumed to be in, if a timezone is not specified as part of
       the date.

AUTHOR
       Copyright 2006 by Joey Hess <id@joeyh.name>

       Licensed under the GNU GPL.

moreutils                                                       2024-02-25                                                           TS(1)
ts -h
Output:
Unknown option: h
usage: ts [-r] [-i | -s] [-m] [format]
Bash script:
while true
do
    date
    sleep 1
done | ts '%.T'
Output:
14:02:41.426643 Sun  7 Jun 14:02:41 CEST 2026
14:02:42.427180 Sun  7 Jun 14:02:42 CEST 2026
14:02:43.431477 Sun  7 Jun 14:02:43 CEST 2026
14:02:44.435760 Sun  7 Jun 14:02:44 CEST 2026
...
For within the second precision: ```bash ts --help ``` <details> <summary>Output:</summary> ``` Unknown option: help usage: ts [-r] [-i | -s] [-m] [format] ``` </details> ```bash man ts | cat ``` <details> <summary>Output:</summary> ``` man: can't set the locale; make sure $LC_* and $LANG are correct TS(1) TS(1) NAME ts - timestamp input SYNOPSIS ts [-r] [-i | -s] [-m] [format] DESCRIPTION ts adds a timestamp to the beginning of each line of input. The optional format parameter controls how the timestamp is formatted, as used by strftime(3). The default format is "%b %d %H:%M:%S". In addition to the regular strftime conversion specifications, "%.S" and "%.s" and "%.T" are like "%S" and "%s" and "%T", but provide subsecond resolution (ie, "30.00001" and "1301682593.00001" and "1:15:30.00001"). If the -r switch is passed, it instead converts existing timestamps in the input to relative times, such as "15m5s ago". Many common timestamp formats are supported. Note that the Time::Duration and Date::Parse perl modules are required for this mode to work. Currently, converting localized dates is not supported. If both -r and a format is passed, the existing timestamps are converted to the specified format. If the -i or -s switch is passed, ts reports incremental timestamps instead of absolute ones. The default format changes to "%H:%M:%S", and "%.S" and "%.s" can be used as well. In case of -i, every timestamp will be the time elapsed since the last timestamp. In case of -s, the time elapsed since start of the program is used. The -m switch makes the system's monotonic clock be used. ENVIRONMENT The standard TZ environment variable controls what time zone dates are assumed to be in, if a timezone is not specified as part of the date. AUTHOR Copyright 2006 by Joey Hess <id@joeyh.name> Licensed under the GNU GPL. moreutils 2024-02-25 TS(1) ``` </details> ```bash ts -h ``` <details> <summary>Output:</summary> ``` Unknown option: h usage: ts [-r] [-i | -s] [-m] [format] ``` </details> <details> <summary>Bash script:</summary> ```bash while true do date sleep 1 done | ts '%.T' ``` </details> <details> <summary>Output:</summary> ``` 14:02:41.426643 Sun 7 Jun 14:02:41 CEST 2026 14:02:42.427180 Sun 7 Jun 14:02:42 CEST 2026 14:02:43.431477 Sun 7 Jun 14:02:43 CEST 2026 14:02:44.435760 Sun 7 Jun 14:02:44 CEST 2026 ... ``` </details>
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Benjamin_Loison/linux#57