[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Why echo without quotes need to glob the file
From: |
Greg Wooledge |
Subject: |
Re: Why echo without quotes need to glob the file |
Date: |
Tue, 13 Dec 2022 09:15:53 -0500 |
On Tue, Dec 13, 2022 at 08:45:36PM +0800, wang yuhang via wrote:
> $ touch 1
> $ echo [--hello]
> 1
> I know the main reason is that my some code is poorly written. from another
> perspective, the builtin command `echo` has nothing to do with file, but it
> still uses regular expressions to glob the file. I don't quite
> understand this behavior。
>
>
> Could you tell me the reason for the original design ?
[--hello] is an example of a "glob", which is a pattern that can match
one or more filenames. Other examples of globs are *.txt or foobar.[ch]
Since your glob is not quoted, the shell expands it to a list of filenames
that it matches in the current directory.
If you want [--hello] NOT to be expanded, put quotes around it. Either
single quotes or double quotes will do.