Why do I get a list of files when I run echo* on the Linux command line?

Asked 2 years ago, Updated 2 years ago, 116 views

For example, on Linux, if there are three files in the current directory:

#ls-l
Total 0
-rw -r --r --1 root root 0 August 31 21:34 2022 AAA
-rw -r --r --1 root root 0 August 31 21:34 2022BBB
-rw -r --r --1 root root 0 August 31 21:34 2022 CCC

If you execute the echo* command, you will see the results.

#echo*
AAA BBB CCC

If the information given to the echo command contains *, it may be interpreted as a file name in the current directory and processed, but why did it behave like this?

What I want to know is how and why the specification was made, and maybe it's not in the documentation, but if you think of * (asterisk) as a "wildcard" or "zero or more characters" meta-character, I don't think it's against my intuition to interpret echo* as just an echo.

#echo*

It would be best if it remained as a document, but if you have any questions about the specification, I would appreciate it if you could let me know.

linux bash zsh

2022-09-29 20:27

3 Answers

In the POSIX shell (bash, dash, csh) command line, * is only interpreted as shell glob. manbash command line will all be the same, but echo

The reason and reason for this would be the word rationale in English, but if there is one, it would be fine if the document appeared around POSIX (just after the specification is confirmed), or it would be just a historical reason.I can only guess why the person who decided the behavior of the globe at first did so.Well, I can guess that regexp might not have been available at the time, but maybe it didn't have enough memory and CPU capacity to implement in shell, or that regular expressions might have been too complicated for users.

Wouldn't it be too inconvenient if you had to type ls.* in more than 0 iterations of any single character to enumerate all the files?


2022-09-29 20:27

Compared to other operating systems (such as Windows), I understand that
For example,

progA*.dat>output

In Windows and DOS, if a wildcard such as *.dat is specified

You can choose the behavior of … (when progA was built)

(Deploying one at a time seems to be because if you deploy all at once, the command line will be punctured.)

In UNIX operating systems, *.dat deployment, including standard input/output (>output portion above), is a "shell" role.
(For your information) There are various deployments such as bash pathname expansion, as well as brace deployment, tilda deployment, parameter deployment, etc.

Then
progA runs on the deployed command line. echo* is treated the same way (even if it is a built-in command or not)

Note: https://ja.wikipedia.org/wiki/ shell


2022-09-29 20:27

The behavior follows @774RR's response, and historically, the initial UNIX command has taken over the same specification


2022-09-29 20:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.