2

I tried the following to no avail and couldn't find any documentation.

read Option
case $Option in
    [1] ) echo "1" ;;
    [2] ) echo "2" ;;
    [$'\n'] ) echo "LF" ;;
    [0] ) exit ;;
    * ) echo "Invalid input" ;;
esac

The following works though.

$ x="                                         
> "
$ if [ "$x" = $'\n' ]; then echo "newline"; fi
newline
  • 1
    You don't need [...] to enclose a single character in any of those cases. – chepner Sep 10 '15 at 20:31
6

Since read only reads a single line of input, sans newline, you just want to check for the empty string:

read Option
case $Option in
    1 ) echo "1" ;;
    2 ) echo "2" ;;
    "" ) echo "LF" ;;
    0 ) exit ;;
    * ) echo "Invalid input" ;;
esac

[1] and 1 (and similarly for the other one-character classes) match the same strings as patterns.

2

read will strip it. The case statement itself works.

#read Option
Option='
'
case "$Option" in
    [1] ) echo "1" ;;
    [2] ) echo "2" ;;
    [$'\n'] ) echo "LF" ;;
    [0] ) exit ;;
    * ) echo "Invalid input" ;;
esac
#Outputs: LF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.