read the ENTER key

Hi ,

I do the following :

]echo "Do you want to say yes or no ?(y/n)[n]:\c"
read ans

here 'n' is the default value.that means if the user press ENTER key then it should be 'n' .
Now how do i know that the user has pressed ENTER key.What will be stored in my variable 'ans'.

No

ans will contain what you typed until the enter key was pressed. If you only press enter, then that variable will be empty.
To do what you want, you need to do something like:

echo "Do you want to say yes or no ?(y/n)[n]:\c"
read ans
case $ans in
n|"")
  echo no;;
y)
  echo yes;;
*)
  echo other;;
esac

I am not sure

"^m" would be stored in variable if you are using Linux

I've modified the code I posted before, have a look at it now. It works in Linux (bash) also.
What I meant with empty was "no printable chars", I'm sorry :slight_smile:

I'm not sure how literally sars meant the question. But here is an interesting thread about reading a single keystroke from a shell script.