6

How can I give space " " as input in shell script ?

Ex :

echo " Enter date for grep... Ex: Oct  6 [***Double space is important for single date***] (or) Oct 12 "
read mdate 
echo $mdate

I get the output as Oct 6 but I want Oct 6.

Kumar
  • 903

1 Answers1

11

You already have Oct  6 in $mdate, your problem is that you're expanding it when printing. Always use double quotes around variable substitutions. Additionally, to retain leading and trailing whitespace (those are stripped by read), set IFS to the empty string.

IFS= read -r mdate
echo "$mdate"

The -r (raw) option to read tells it not to treat backslashes specially, which is what you want most of the time (but it's not the problem here).