Sunday, April 28, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 483  / 2 Years ago, thu, february 24, 2022, 12:24:35

I want to use user input of special characters in conditional statement.
I tried:


read -p "press arrow up" foo
if [ $foo == "^[[A" ]; then echo yes; fi

It did not do what I wanted it to do.


More From » command-line

 Answers
6

In bash, you could use ANSI-C quoting


#!/bin/bash

read -p "press arrow up " foo

if [ "$foo" = $'e[A' ]; then
echo 'yes'
fi

For a more nuanced version, see BASH question: using read, can I capture a single char OR arrow key (on keyup)


[#388] Thursday, February 24, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
icielight

Total Points: 158
Total Questions: 92
Total Answers: 93

Location: San Marino
Member since Thu, Jun 30, 2022
2 Years ago
;