Friday, October 28, 2022

[SOLVED] How to use nohup with curly braces?

Issue

I try to run the following command (ref.) using nohup, which basically separates stdout and stderr into two processes.

{ foo 2>&1 1>&3 3>&- | sed -u 's/^/err: /'; } 3>&1 1>&2 | sed -u 's/^/out: /'

The foo script is like below.

#!/bin/bash
while true; do
   echo a
   echo b >&2
   sleep 1
done

This is the test result.

$ nohup { foo 2>&1 1>&3 3>&- | sed -u 's/^/err: /'; } 3>&1 1>&2 | sed -u 's/^/out: /' >/dev/null 2>&1 &
-bash: syntax error near unexpected token `}'

Solution

That's syntatically impossible. But you can wrap your {} in a sh -c cmd:

nohup sh -c 'foo 2>&1 1>&3 3>&- | sed -u "s/^/err: /"'

Notice I change the single quote for sed to double quote.



Answered By - DannyNiu
Answer Checked By - David Goodson (WPSolving Volunteer)