Why Does 'Echo *' Give Different Results Than 'Ls *'?

For example, if you are in home directory and type ls *, you will have all the directories listed, and below the name of the listed directory, the files that it contains. On the other hand, if you type echo * the command will list just the directories, and not the directories and content that they have. Why is that?

1

1 Answer

In both cases the * gets expanded by the shell.

echo than just prints the parameters resulting from the expansion.

ls checks for every parameter if it is a directory and lists its contents if it is one.

Your Answer

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

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest