LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 10-01-2009, 01:16 AM   #1
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Not able to find difference between echo and ls


Hi all...

I have text1, text2, text3 in a directory

[vinay@localhost temp]$ echo text*
text1 text2 text3
[vinay@localhost temp]$ ls text*
text1 text2 text3
[vinay@localhost temp]$ echo "test*"
test*

Here I am not able to understand why the first echo command is taking ls commands feature ??

Please help me ...
Thanks in advance...
 
Old 10-01-2009, 01:30 AM   #2
bobloblian
LQ Newbie
 
Registered: Dec 2008
Location: Yukon, Canada
Distribution: Debian/Ubuntu
Posts: 20

Rep: Reputation: 1
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.
 
Old 10-01-2009, 01:35 AM   #3
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by bobloblian View Post
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
 
Old 10-01-2009, 01:37 AM   #4
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Nagpur, India
Distribution: Cent OS 5/6, Ubuntu Server 10.04
Posts: 4,629

Rep: Reputation: Disabled
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.
 
Old 10-01-2009, 01:42 AM   #5
generalmills
LQ Newbie
 
Registered: Jul 2009
Location: California
Distribution: Fedora
Posts: 3

Rep: Reputation: 1
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.
 
Old 10-01-2009, 01:43 AM   #6
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by linuxlover.chaitanya View Post
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 ??
 
Old 10-01-2009, 01:51 AM   #7
generalmills
LQ Newbie
 
Registered: Jul 2009
Location: California
Distribution: Fedora
Posts: 3

Rep: Reputation: 1
The regular expression seems to be matching anything in the current directory. It's got to match something...
 
Old 10-01-2009, 01:53 AM   #8
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Nagpur, India
Distribution: Cent OS 5/6, Ubuntu Server 10.04
Posts: 4,629

Rep: Reputation: Disabled
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.
 
Old 10-01-2009, 01:59 AM   #9
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by linuxlover.chaitanya View Post
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
 
Old 10-01-2009, 03:27 AM   #10
ilu_nishant
LQ Newbie
 
Registered: Sep 2009
Posts: 11

Rep: Reputation: 1
Adding more on this..

Hi

Its actually an interview question mostly asked.
except ls command is there any other way to list the files of a directory in Linux?

1 ) ls
2 ) dir
3 ) echo *

Special meaning of metacharacter ( * ) is "not" being ignored when used out of double and single quotes.

Please note there no way to list hidden files using echo * command.
but this you can do by adding option -a with ls and dir command in Linux.

cheerzz!!
 
Old 10-01-2009, 05:27 AM   #11
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
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:

Code:
function firstparm() { echo $1; }
firstparm *
 
Old 10-01-2009, 05:56 AM   #12
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by CroMagnon View Post
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:

Code:
function firstparm() { echo $1; }
firstparm *
Very Helpfull CroMagnon...Thanks a lot
 
Old 11-30-2009, 06:37 AM   #13
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
It's called "Pathname Expansion" & you can find the formal explanation in the bash man page by searching for "After word splitting,".
 
Old 11-30-2009, 08:16 AM   #14
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by archtoad6 View Post
It's called "Pathname Expansion" & you can find the formal explanation in the bash man page by searching for "After word splitting,".
In the GNU Bash Reference manual it is called filename expansion and you can find more than you ever wanted to know about it here.
 
Old 11-30-2009, 11:16 AM   #15
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to find the difference betwwn two file and place the difference to other kittunot4u Linux - General 3 07-19-2010 05:26 AM
How to : Find the difference between 2 days ? avklinux Programming 1 02-27-2009 08:16 PM
what is the difference between these commands ls * and echo * izrafel Linux - General 5 06-12-2007 01:49 PM
ls | echo, I got blank, why can't echo take the 2nd seat in a pipeline? elinuxqs Linux - Newbie 6 11-24-2006 09:25 AM
How can i find the difference for directory alice95089 Linux - Software 4 02-07-2005 09:49 AM


All times are GMT -5. The time now is 01:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration