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
.
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
.
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).
read toto ; echo "$toto"
: when i enter______this____
(_ are spaces) it outputsthis
(I believe this is not something done by read, but by the shell's line policy? how to change it? stty raw before the read, or something?) – Olivier Dulac Oct 09 '14 at 07:44IFS=
. This is about the shell, it has nothing to do with terminal settings. – Gilles 'SO- stop being evil' Oct 09 '14 at 07:49OLDIFS="$IFS" ; IFS="" read toto ; echo "__${toto}__" ; IFS="${OLDIFS}"
– Olivier Dulac Oct 09 '14 at 07:50IFS
for the duration of theread
call. – Gilles 'SO- stop being evil' Oct 09 '14 at 07:51IFS='' read toto
will change IFS just for the duration of the call to read ... (I know this, usually...) – Olivier Dulac Oct 09 '14 at 07:53