Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I would expect that is because of the special character *. the shell will interpret this as a regular expression, when you put it in quotes, it becomes a string.
I would expect that is because of the special character *. the shell will interpret this as a regular expression, when you put it in quotes, it becomes a string.
still..why it should display filenames ? That is my question...i.e, it is taking ls command's feature
No it is not hijacking any features of ls.
You could understand that. Without any options to both the commands run those inside a directory and see what is the output.
Well this is an interesting find. I didn't even know echo could be used this way...
I believe the reason echo is behaving this way is because of the "*" after text. Echo first looks for any file with text in the beginning of its name, and prints it out as a string. I could be wrong, but that's my 0.02 cents.
No it is not hijacking any features of ls.
You could understand that. Without any options to both the commands run those inside a directory and see what is the output.
Thanks for your reply...
I am running both ls and echo without options above as you have mentioned...But why echo * or echo text* is displaying file names ??
DO NOT even use "*" as the option and then you will see. * matches everything in the directory and thats why it is giving the output. echo is used to output anything on screen and this what echo is doing.
Just type echo and press return and then see.
DO NOT even use "*" as the option and then you will see. * matches everything in the directory and thats why it is giving the output. echo is used to output anything on screen and this what echo is doing.
Just type echo and press return and then see.
Thanks to everyone...Finally i understood the concept
None of the explanations above seem very clear to me, and I am still not sure if some people understand.
The shell intercepts certain characters and performs operations on them BEFORE the command runs. In the case of "echo text*", bash changes "text*" to 'all files that start with text' before it calls echo. So in this case, the shell changes the command you've written to "echo text1 text2 text3". IOW, expanding patterns is not actually understood by ls, and you can see this by entering a directory with many many files and typing "ls *" - you can actually exceed the maximum command line length this way (I needed around 5000 files with quite long names). Escaping the * by surrounding it with double quotes or putting a backslash in front of it stops bash from doing this:
Code:
echo text*
text1 text2 text3
echo text\*
text*
You can also see this with a small shell function:
None of the explanations above seem very clear to me, and I am still not sure if some people understand.
The shell intercepts certain characters and performs operations on them BEFORE the command runs.In the case of "echo text*", bash changes "text*" to 'all files that start with text' before it calls echo. So in this case, the shell changes the command you've written to "echo text1 text2 text3". IOW, expanding patterns is not actually understood by ls, and you can see this by entering a directory with many many files and typing "ls *" - you can actually exceed the maximum command line length this way (I needed around 5000 files with quite long names). Escaping the * by surrounding it with double quotes or putting a backslash in front of it stops bash from doing this:
Code:
echo text*
text1 text2 text3
echo text\*
text*
You can also see this with a small shell function:
In the strict sense, echo is not displaying file names. It is just displaying a string that it receives. Echo, unlike ls, has no concept of file names or whatever. The expansion happens before echo even gets into scene, to start with. All that echo receives is a string, and it effectively echoes that screen to stdout. The fact that that screen matches a list of file names is purely coincidental, and is due to the preliminary expansion that the shell does before passing the arguments to echo.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.