Please explain why 1 4 5 6 is displayed for the last four echo statements? I hit this by accident once, but I am now curious as to why this behavior occurs.

These statements work as expected (for me).

$ echo [ 9876543210 ]  
[ 9876543210 ]

$ echo [237890]  
[237890]

These echo statements consistently display 1 4 5 6. Is there something special about these numbers?

$ echo [9876543210]  
1 4 5 6

$ echo [abcd9876543210ghi]  
1 4 5 6

$ echo [-123456-]  
1 4 5 6

$ echo [-7654321-]  
1 4 5 6

Thanks!

  • The possible duplicate is related and helpful, but not a duplicate. The possible duplicate is from the perspective of an rm command. This question is from the perspective of a perceived "weird behavior" of an echo command. The underlying answer for both is globbing. Someone searching for issues with an echo command would not easily find the rm question, but would more likely land here.
  • 15
    you have 4 files in your current directory, named: 1 4 5 and 6 – Jeff Schaller Feb 27 '17 at 18:49
  • 5
    Lol, the bot at metasmoke.erwaysoftware.com/post/58973 flags this post as spam because it "has a phone number in the title"... really? – Carlo Wood Feb 27 '17 at 22:57
  • 1
    @CarloWood, ten consecutive digits matches a United States phone number with area code. Written using normal formatting, it would be 987-654-3210. – Mark Feb 27 '17 at 23:39
  • 1
    Note that if you turn on nullglob, your second example (echo [237890]) will actually print nothing, which could have been a clue that you were seeing glob-expansion in the other examples. – Kyle Strand Feb 28 '17 at 0:46
  • 3
    Possible duplicate of Working of rm/ls with [0-9] – muru Feb 28 '17 at 5:11
up vote 33 down vote accepted

The open bracket [ is a special character to the shell; it opens up a pattern matching algorithm that says "match any of the characters inside the brackets". Because you have 4 files named as: 1, 4, 5, and 6 in your current directory, when the characters inside the brackets contain any of those digits, your shell replaces the pattern-match with those filenames. When you instead use echo [ 9876543210 ] you are calling echo with 3 parameters: [, 9876543210, and ].

You should quote your echo statement's parameters to prevent the shell from seeing it as a pattern matching request.

$ echo '[9876543210]'
[9876543210]

(or remove the files named 1, 4, 5, and 6 -- but that's a workaround to demonstrate the behavior, not a fix).

  • 4
    Perhaps it would be better to say that echo [ 987 ] calls echo with three parameters than to even mention test? – dhag Feb 27 '17 at 18:57
  • 2
    it would indeed! I over-answered :( – Jeff Schaller Feb 27 '17 at 18:57
  • 1
    * I also like your first answer version, as I initially ran across this while trying out echo statements of various tests. – MikeD Feb 27 '17 at 21:58
  • 1
    The original answer is not even true. He wrote: echo [ 9876543210 ] . No invocation of [ (thus test) is done. – knezi Feb 28 '17 at 8:57
  • The key point is that the argument to echo is interpreted differently by bash depending on whether or not there are spaces between the square brackets and what's inside them. [123] vs. [ 123 ]. – jskroch Mar 14 '17 at 15:50

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.