basename in pipes

basename in pipes

am 02.12.2004 18:42:43 von John Weatherwax

Hello,
This may seem like a dumb question ... but why doesn't the following work?

pwd | basename

or

echo `pwd` | basename

Is it true that one can't run basename in a sequence of pipes? Why not?

Thanks for any help,

John Weatherwax


PS.

$ uname -a
Linux linux 2.4.21-99-smp4G #1 SMP Wed Sep 24 14:13:20 UTC 2003 i686
i686 i386 GNU/Linux

Re: basename in pipes

am 02.12.2004 18:51:52 von Adam Price

On Thu, 02 Dec 2004 12:42:43 -0500, John Weatherwax wrote:

> Hello,
> This may seem like a dumb question ... but why doesn't the following work?
>
> pwd | basename

Because basename does not read from stdin it expects an argument on the
command line.
You probably want

basename `pwd`

Adam

Re: basename in pipes

am 02.12.2004 18:55:50 von Stephane CHAZELAS

2004-12-02, 12:42(-05), John Weatherwax:
> Hello,
> This may seem like a dumb question ... but why doesn't the following work?
>
> pwd | basename

Because basename expects an argument and reads nothing from its
standard input. Generally only text filtering utilities read
their data from their standard input.

basename "`pwd`"

or:

pwd | xargs basename

(will fail if the pwd contains blanks or quotes or backslashes).

--
Stephane