Why can't I type the first argument passed to my program?

I made simple code in c:

#include <stdio.h>
int main( int argc, char* argv[] ) {
    printf( "Hello, just wanted to say: %s.\n", argv[1] );
    return 0;
}

and made two programs out of it - app1and app2.

Now I'm experimenting with pipelines:

./app1 Bye | ./app2

and the output placed in %sis equal (null)instead of the expected "Hello". Why?

+3
source share
1 answer

It seems you misunderstood how the pipe works. When you use a channel like you did, the output is app1used stdinfor app2, but not the command line argument for app2.

This is true:

stdin->app1->stdout >----------> stdin->app2->stdout
                        pipe
+10
source

All Articles