[gllug] echo
Richard Cohen
vmlinuz at gmail.com
Wed Apr 20 07:48:28 UTC 2005
On 4/20/05, Steve Nelson <sanelson at gmail.com> wrote:
> Hi all,
>
> Just a quickie - I've just discovered (don't laugh!) that 'echo'
> behaves as follows:
>
> $ echo beer
> beer
> $ echo beer*
> beer*
> $ echo file
> file
> $ echo file*
> file1 file2 file_for_testing
>
> ie if there is a file matching that which I ask the shell to echo, it
> tells me those files, rather than echoing file*.
>
> I appreciate that this has to do with shell expansion, such that I can do:
>
> $ echo 'file*'
> file*
>
> My question is why this is connected with filenames. Why does the
> shell look for matching filenames? ie - Why does echolists all files
> beginning with file. The echo(1) manual page doesn't help.
The point is that what you type isn't what gets sent to echo. The
shell performs expansion *before* passing the command-line onto the
command, so when you type "echo beer*" the beer* doesn't match
anything, so echo sees "beer*" as the argument list. When you type
"echo file*", the shell expands the file* and passes "file1 file2
file_for_testing" onto echo as its command-line argument list.
It's trivial to test this with a tiny program in any language which
lets you access the command-line args:
-----
#! /usr/bin/python
import sys
for i in range(len(sys.argv)):
print "arg number %d is %s" % (i, sys.argv[i])
-----
> I'm sure this is blindingly obvious, but I'm curious!
>
> S.
Cheers
Richard
--
Gllug mailing list - Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
More information about the GLLUG
mailing list