I'm trying to create an interactive bash script, where i can call given options from 1-n or just them like commands.
It will end up with a simulated prompt and 'read' is used to get the input, ofc.
But, if i type too big of a text, it will return to the beginning of the line and overwriting the prompt as i type.
Prompt is color coded and if i remove color escapes it will be just fine. But i like colors :)
Script prompt is like this:
NOC=$(echo -en '\033[0m') # Default
RED=$(echo -en '\033[00;31m')
YELLOW=$(echo -en '\033[00;33m')
CYAN=$(echo -en '\033[00;36m')
OPROMPT="${RED}[Admin${CYAN}@${RED}bulletproof]#${NOC}"
until [ ! -z "$MCHOICE" ]; do
read -p "${OPROMPT} " -e MCHOICE
done
What am i supposed to do to stop this behavior ? Cant seem to figure it out. If i use echo or printf to display the prompt, it will erase it if i type something and then hit backspace to correct it.
If I'm not clear, here's an example: This is the prompt (just picture it colored :P)
"[Admin@bulletproof]# "
...now here comes typing commands:
"[Admin@bulletproof]# vpn start my"
... now i continue typing
"domain.lanletproof]# vpn start my"
When it should be:
"[Admin@bulletproof]# vpn start mydomain.lan"
I also noticed that the buffer is different depending on the terminal window size. It doesn't start cutting off at same point when i have the terminal maximized, but has longer "tolerance"
EDIT: Just figured a way to substantially improve 'buffer' about this one.
If i set the code like this:
until [ ! -z "$MCHOICE" ]; do
printf "$OPROMPT"
read -p " " -e MCHOICE
done
It will allow me to input a lot longer text... Don't know why..
\[
and\]
so that bash knows they don't occupy any space on the screen.\001
and\002
to surround the escape sequences in theread
prompt. I've written up an answer.