0

Given the following contrived example which intent is to only erase the subcommand's /dev/tty:

result="$(
  tput smcup > /dev/tty
  tput cup 0 0 > /dev/tty
  echo 'meh' > /dev/tty
  echo 'nay' > /dev/stderr
  echo 'yay' > /dev/stdout
  tput rmcup > /dev/tty
)"; echo "result=[$result]"

It outputs:

result=[yay]

However, the stderr of nay is no longer kept in the display. How can I make sure that stderr is kept displayed? Such that the output is:

nay
result=[yay]

The real use case is where the subcommand asks the user a series of questions and calculates a response that is sent to stdout, and if an error occurs the error is sent to stderr. Once the subcommand is completed the intent is for the question interaction to be wiped, the stdout to be stored, and any stderr to be displayed.

1 Answer 1

1

If you write to stderr the value isn't captured into the variable. This behaviour is by design. If you want to keep nay either you need to write it to stdout or you need to reopen stderr as stdout (which is much the same thing).

For the purpose of reducing confusion, this behaviour has nothing to do with tput smcup or any other terminfo characteristic.

Furthermore, the only value placed into $result will be the output written to stdout. Every other output will be written immediately to the terminal. The segment could have been far more straightforwardly written like this:

result='yay'

tput smcup
tput cup 0 0
echo 'meh'
echo 'nay'
tput rmcup

echo "result=[$result]"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .