1

Why are the outputs different?

touch a aa bb ccc

echo *

a aa bb ccc // I understood, * means matches zero or more

*

aa bb ccc // I'm confused.

echo ?

a //OK

?

a aa bb ccc // confused again.

*?

aa bb ccc // very confused

* *

a aa aa bb bb ccc ccc // very very confused

I wonder what's the difference between using echo and not using it.

1 Answer 1

3

Each line you write must have a command, usually the first word.

To get something printed, a common command is echo.

If the pwd (present working directory) has files a, aa, bb, and ccc.
Then, this command will print all files in the directory:

 $ echo *
 a aa bb ccc

And this command will print all files in the pwd that have one character:

$ echo ?
a

However, if you do not provide a command, the first "word" (most of the time: up to the first white space) will be taken to be the command.

If you only supply the asterisk *, the first word that results from the expansion of the asterisk will be used as the command to execute.

If the command a does not exist, an error will be printed:

$ *
bash: a: command not found

If there is a command called a, it will be called with the three arguments aa bb ccc.

As you provide no more detail, we can only make a wild guess of which command is being executed with the name a (it is not a default command). Also we don't know which shell are you using.

But all your examples show that the first a is missing:

$ *
aa bb ccc              ### missing first `a`.
$ *?
aa bb ccc              ### missing first `a`.
$ * *
a aa aa bb bb ccc ccc  ### missing first `a`.

Except for:

$ ?
a aa bb ccc

Of which is difficult to make any conclusion as we don't know what command is a.

2
  • 1
    ... possibly something like function a { \ls $@; } such that * expands to ls aa bb cc while ? expands to just ls May 21, 2016 at 22:29
  • 1
    @steeldriver Yes, that's possible. If the OP could execute type a and tell us the result we will probably know.
    – user79743
    May 22, 2016 at 3:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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