2

I have this code in an infinite loop:

read -a output
echo ${output[@]}

When the code runs the screen might look like this:

Hello, World # line where value was given to output (I want this gone)
Hello, World # echoed output
[] # <-- cursor waiting for next input

Is there a way to clear the line where the user inputs the value? Like, as in get rid of it totally (not just changing the line to whitespace that will still leave a gap in the desired output)?

2 Answers 2

2

Use tput with the cuu1 and dl1 capabilities.

echo "foobar42"
echo "$(tput cuu1)$(tput dl1)baz123"
Sign up to request clarification or add additional context in comments.

2 Comments

@Jonathan: In some cases it's better to use el (erase to end of line) instead of dl1 (delete line) since the latter will cause text below that line to scroll up. Also, it's not necessary to echo the output of tput using command substitution. tput cuu1; tput cr; tput el; echo baz123 (the cr ensures that you're at the beginning of the line)
Thanks, Dennis. The dl1 method left unwanted characters before the text after I redirected it to a log file. Your way works like a charm.
0

Why not just remove the echo ${output[@]}? Since you're not quoting your array variable, this seems to be the same result as if you were to use tput hax.

1 Comment

The actual output isn't just ${output[@]}, there is additional formatting stuff concatenated to the contents of the array. Thanks, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.