1

I have written two simple C programs, program1 prints out a predefined string and program2 takes a string as an argument and writes that string into a file.

What I'm trying to do is to take the output of program1 and pass it to program2. I've been through bash tutorials and the only solution I could find was:

program1 | program2

This is supposed to work but I get a segmantation fault. So I tried this and it works.

program1 | program2 abc

As you can guess this results in an output file containing the string "abc". program1 | program2 seems straightforward but I guess I'm missing something here?

3
  • 1
    Post your script, you might need to store the output into a variable instead of a straight pipe. Commented Dec 21, 2011 at 17:40
  • 2
    As he mentioned, his program2 takes the string as an argument, not STDIN. This is the cause of the problem. Commented Dec 21, 2011 at 17:43
  • Yes, I modified second program so it takes the string from stdin and my original script works. Thanks for all the answers. Commented Dec 21, 2011 at 18:02

3 Answers 3

4

| makes the program to the right read (as STDIN) the STDOUT of the program on the left.

But your program2 does not read STDIN at all. It reads the arguments (which are NOT STDIN).

You should do:

program2 `program1`

Bash evaluates program1 (when it sees the backquotes), and passes it as an arg to program2.

On my keyboard the backtick (`) is to the left of the "1" key, and above my LEFT TAB key.

EDIT: If the string output of program1 contains spaces and you want the entire string to be interpreted as one argument, quote the string with "" or '':

program2 "`program1`"
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks this one seems to work. I modified the programs so there are two outputs of program1 and program2 takes two arguments. It seems to work with multiple arguments too.
I think that it's better to quote the backticks with double apostrophes like this: program2 "`program1`". Otherwise if the output of program1 contains spaces, it won't be considered as one argument, but as multiple arguments. echo `cat aFile` is a good example of that.
Also, I'd mention that program2 "$(program1 )" is a valid alternative.
@lef2: You are right. I will leave out "$(program1)" because it is there in one of the other upvoted answers. But I will mention the " ". Actually, even ' ' is valid, and a stronger escape than " " (although here it won't matter).
2

I think this should also work:

$ program1 | xargs program2

1 Comment

+1. Yes, this will also work. I like backquotes more though as it is built into bash. xargs is a separate program.
1

You said, “program2 takes a string as an argument.”

The pipe | system redefines the program's standard input, not argument.

To take the output of program1 as an argument to program2, use:

  program2 $(program1)

The $() (also the back-tick ` can be used, but there are reasons to avoid this) takes the output of a program and adds it to the current line, then re-evaluates it; so if program1 prints out "foo", the command to be run is program2 foo

1 Comment

Note, if your output will be more than one word, you may want to use quotes. program2 "$(program1)" or something like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.