-1

I'm a beginner writing Bash scripts so I'm confused on the architecture of pipes. For the basic commands such as grep for example, did pipes have to be somehow configured and programmed to be allowed into the command? Or do pipes just automatically pass in the outputs as the first argument to whatever is being piped to?

My main question is say I have a script that takes in text as the first argument using something like my_script.sh text_file. Would pipes work automatically if I were to do echo Hello | my_script.sh or would I have to follow some interface to allow pipes.

StoneMan
  • 423
  • 3
  • 18
  • 1
    `echo Hello | my_script.sh` will write Hello to the standard input of the bash script which could be accessed through the `read` command. If you want it as an argument use `my_script.sh $(echo Hello)` which in fact is equal to `my_script.sh Hello` – msrd0 Jun 20 '15 at 21:04
  • 1
    and your script can't allow pipes as it can't forbide pipes. – msrd0 Jun 20 '15 at 21:04

1 Answers1

2

In Unix like systems, everything is a file.Funneling output of a command (again, it is a file) to another as an input is called pipe lining (or piping in short).

So when you see a set of commands as follows:

history | grep ls | less

history : returns list of bash commands, entered from a terminal. Its output is fed into grep as an input. grep ls : finds all lines with "ls" letters. (Think about it as some sort of filtering mechanism). And again, its end result is given to less as an input less : show screen-full of information at a time and waits for user to hit a key.

Back to your question: In order to write a script which gets input from standard input, you have to keep in my mind to read from there. If your script is written so, so yes, you can do it.

Here are a couple of examples, where you can find how to write such scripts.

Hope it helps =]

Community
  • 1
  • 1
Alp
  • 3,027
  • 1
  • 13
  • 28