Add prefix to output #57

Closed
opened 2025-01-14 10:43:40 +01:00 by Benjamin_Loison · 2 comments
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).
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