Operating System - HP-UX
1833589 Members
3883 Online
110061 Solutions
New Discussion

echo Whitespace stripping....

 
SOLVED
Go to solution
Richard Briggs
Regular Advisor

echo Whitespace stripping....

execute
^^^^^^^
date +%b" "%e" "%I:%M:

and you get something like:

Jun 6 08:38:

(depending on what day and time you execute it)

Now execute:
^^^^^^^^^^^^
echo `date +%b" "%e" "%I:%M:`
or
echo $(date +%b" "%e" "%I:%M:)

and you get:

Jun 6 08:38:

Did you notice the difference in the output? Look again, side by side:

************************
Jun 6 08:38:
Jun 6 08:38
************************

Why is my whitespace infront of the day being stripped away when echo (or print) prints the output of the date command!!?

rab

#find / -name coffee | cup < cream
5 REPLIES 5
Thierry Poels_1
Honored Contributor
Solution

Re: echo Whitespace stripping....

Hi,

echo drops multiple whitespaces.

echo `date +%b" "%e" "%I:%M:`
results in :
echo Jun##6#03:46:
(replace blanks by # for visibility)
which outputs:
Jun#6#03:46:


If you want to preserve the blanks you should add quotes:

echo "`date +%b' '%e' '%I:%M:`"

regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Thierry Poels_1
Honored Contributor

Re: echo Whitespace stripping....

hmmm, sorry bad definition:
It's not really echo that drops the duplicate blanks, but rather the shell.
echo uses arguments like any other program, so to preserve blanks in the arguments, add quotes.

regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Richard Briggs
Regular Advisor

Re: echo Whitespace stripping....

thanks!
#find / -name coffee | cup < cream
Richard Briggs
Regular Advisor

Re: echo Whitespace stripping....

even this web post stripped the white spaces!!! sheesh.

;-)
#find / -name coffee | cup < cream
Martin Johnson
Honored Contributor

Re: echo Whitespace stripping....

Yes, I hate it when the software is too smart for its own good. I can't stand Microsoft Word!!!!


Marty