2

I am encountering unexpected behavior I don't yet understand. In bash on OSX I get different outputs for the two following commands:

echo $(date)
Tue May 2 00:24:02 EDT 2017

echo "$(date)"
Tue May  2 00:24:10 EDT 2017

The quoted version adds a space before the day number.

What's going on?

Paul R
  • 208,748
  • 37
  • 389
  • 560
THX1137
  • 903
  • 6
  • 15
  • See [this question](http://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) for a gallery of effects caused by not quoting when using `echo` (command expansion and variable expansion are affected in the same way) – that other guy May 02 '17 at 04:41

4 Answers4

4

Your date command prints an extra space before the day-of-month if the day-of-month is a single digit.

Here's your first command:

echo $(date)

Since you didn't put double-quotes around $(date), the shell performs word-splitting on the output of date, and passes each word as a separate argument to echo. In word-splitting, the shell considers two consecutive spaces to mean the same thing as a single space. So the shell “eats” the extra space emitted by date.

Here's your second version:

echo "$(date)"

Since you put double-quotes around $(date), the shell does not perform word-splitting on the output of date. It passes the entire output of date, except for trailing newlines, as a single argument to echo. So the shell preserves the extra space that was output by date.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 1
    If not obvious from this description, `date` outputs two spaces and it's the unquoted version that strips a space, not the quoted version that adds one. – that other guy May 02 '17 at 04:44
  • Thank you @thatotherguy. I have incorporated your feedback into my answer. – rob mayoff May 02 '17 at 04:47
  • Thank you all for clear answers! You all explained it well in different but useful ways. This is a gotcha that got me on a project I'm on where I was grepping for a string with the extra space. I'm glad to understand this gotcha before it got me on something more serious! – THX1137 May 02 '17 at 18:37
3

With echo "$(date)" you are passing a single argument to echo, and this is simply echoed verbatim. Without the quotes you are passing a a number of arguments, separated by white space, and each of these is then echoed with a single space between each argument, which effectively converts any multiple space separators into a single space.

Paul R
  • 208,748
  • 37
  • 389
  • 560
2

If you want to see what is exactly date's output

$ date | xxd
0000000: 5475 6520 2032 204d 6179 2032 3031 3720  Tue  2 May 2017 
0000010: 3030 3a34 353a 3134 2045 4454 0a         00:45:14 EDT.

you can see 2 spaces

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • Great to know this one! I was actually trying to achieve this output when I was trying to troubleshoot the issue. – THX1137 May 02 '17 at 18:39
1

The output of $(date) is Tue May 2 00:24:10 EDT 2017 with two spaces. Under bash and echo happens the same as with:

echo "x  y"

vs

echo x  y

The first version prints both spaces, the second just one space, because bash is splitting the arguments along whitespaces (\w+) and echo prints each argument with only one space as separator.

jboi
  • 11,324
  • 4
  • 36
  • 43