0

For example I have something written like this

read -p "Enter a character: " variable
if [ $variable -eq 'A' ];

I'm getting an error that says integer expression expected, but I don't want to use an integer variable I want to use a character?

0

1 Answer 1

2

This works for me on RHEL 6.

#!/bin/bash

read -rN1 -p "Enter a character: " variable; echo>&2
if [ "$variable" = 'A' ]; then
  echo Capital A
else
  echo something else
fi

Though if you're going to compare against several characters, you may want to use a case construct instead:

case "$variable" in
  (A)   echo Capital A;;
  (b)   echo Lower case B;;
  ('\') echo Backslash;;
  (*)   echo something else;;
esac
2
  • Does the == work specifically for character variables? I think I've tried that with integer variables and the program would not work unless I used -eq. Commented Aug 20, 2020 at 5:31
  • refer to stackoverflow.com/questions/20449543/…. == is a bash-specific alias for = and it performs a string (lexical) comparison instead of a numeric comparison. eq being a numeric comparison of course. Commented Aug 20, 2020 at 5:37

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.