Reputation: 83
Hello I want to read a command and print it on some line.
Example:
I use a function where I read values into a
and b
then echo these values and the result of some operation:
1. $ 5 plus 5
2. $ = 10
On 1. line I am reading to variables a
and b
, after that I hit enter and echo
the result but on a new line. Read command need enter and enter in terminal give you new line.
How it should look like:
1. $ 5 plus 5 = 10
I tried to use sed with no luck.
EDIT: I am using simple:
$read a b c
#a=5 b=plus c=5
Upvotes: 0
Views: 436
Reputation: 246877
Using ANSI escape codes to go up one line and clear the line:
#!/bin/bash
read -p 'equation: ' operand1 operation operand2
# magic to transform "plus" to "+" left to the reader
let answer="$operand1 $operation $operand2"
echo -e "\033[F\033[K$operand1 $operation $operand2 = $answer"
ref: https://en.wikipedia.org/wiki/ANSI_escape_code
Upon reflection, I'd break that into 2 statements for some clarity:
echo -ne "\033[F\033[K" # move the cursor and clear the line
echo "$operand1 $operation $operand2 = $answer"
Upvotes: 1