I'm creating a sort of interactive tutorial that works like the following:
echo "What do you press if you want to move one word backwords in bash?"
read ans
if [ "$ans" == "ESCb" ]; then
echo RIGHT!
else
echo WRONG!
fi
Now, how can I input the ESC character (ASCII 27 decimal) in string literal? ESC certainly doesn't work.
I understand I probably better off use another language, but this is an assignment and it's required to be in bash script.
You have a couple of ways:
Use bash's ANSI C expansions:
if [ "$ans" == $'\eb' ];
^[.Use printf, which can interpret various character sequences:
$ printf '%b' '\e' | od -c
0000000 033
0000001
$ printf '%b' '\033' | od -c
0000000 033
0000001
$ printf '%b' '\x1B' | od -c
0000000 033
0000001
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With