Topic
  • 2 replies
  • Latest Post - ‏2007-06-13T20:21:45Z by SystemAdmin
SystemAdmin
SystemAdmin
2364 Posts

Pinned topic command substitution vs Pipes

‏2007-06-12T22:20:06Z |
Hello,

I am reading some Linux guide and wondering why the following pipe does not work:

cat <filename> | ls -l

While the following command subtitution actually does he job:

ls -l `cat <filename>`

<filename> is a file containing a list of directories.

Thanks for your time.

fred.
Updated on 2007-06-13T20:21:45Z at 2007-06-13T20:21:45Z by SystemAdmin
  • ishields
    ishields
    988 Posts

    Re: command substitution vs Pipes

    ‏2007-06-13T06:05:35Z  
    You can only pipe stdout to a [i]filter[/i] which is a command that accepts input from stdin. The [b]ls[/b] command doesn't work as a filter. The man page for a command will tell you whether the command accepts input from stdin.

    By the way, your second example will fail if you have blanks in any of your file names, for example a file called "a b c". Try
    [b]echo "a b c" > "a b c"
    cat "a b c"
    ls -l `cat "a b c"`[/b]

    Ian Shields
  • SystemAdmin
    SystemAdmin
    2364 Posts

    Re: command substitution vs Pipes

    ‏2007-06-13T20:21:45Z  
    • ishields
    • ‏2007-06-13T06:05:35Z
    You can only pipe stdout to a [i]filter[/i] which is a command that accepts input from stdin. The [b]ls[/b] command doesn't work as a filter. The man page for a command will tell you whether the command accepts input from stdin.

    By the way, your second example will fail if you have blanks in any of your file names, for example a file called "a b c". Try
    [b]echo "a b c" > "a b c"
    cat "a b c"
    ls -l `cat "a b c"`[/b]

    Ian Shields
    Thanks very much for your time. Great introduction guides for new Linux users like me.

    Fred