Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given tree /some/directory | print.sh

Is there any way for print.sh to know that "tree -f /some/directory" was called, without trying to parse pid processes?

share|improve this question
For what purpose? Why does print.sh need to know what program is sending data? What will you do differently in this case: print.sh < somefile? – glenn jackman Apr 17 '14 at 17:05
print.sh will log the input, as well as format/output as part of a custom header – vol7ron Apr 17 '14 at 17:22
and you want the "originating command" to be part of the log? No, print.sh cannot know that innately, it only knows it has stdin connected from somewhere. You'll have to go hunting for it. Or pass the command and the print.sh script will invoke it: print.sh 'tree /some/dir' – glenn jackman Apr 17 '14 at 17:31
@glennjackman right. Exactly my line of thinking, which is why I'm looking for the best way. Preference would be not to quote. I'd rather sniff the shell history (if it's available at that point) – vol7ron Apr 17 '14 at 19:31

In general this information cannot be obtained. print.sh can easily determine that its input is coming from a pipe, but not what is at the other end of the pipe. The first process may even have terminated, with its output fully contained in the pipe buffer. In this case, not even traversing the process list will give you any information.

Whatever it is you're trying to, you need to look for a solution in a different solution. This is probably going to involve having the script that's calling the pipe do the logging the first command.

share|improve this answer
Yes, I think you and Glenn Jackman are both correct. Thinking it wasn't possible was the first conclusion I came to, but I don't consider myself a Linux guru. I had hope that the Kernel might pass that information along in some way, so scripts could behave differently based on origination of STDIN -- logging was just one of the many potential uses. – vol7ron Apr 18 '14 at 14:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.