2

From what I know, parameters you pass to a command, goes to it's STDIN stream.

so this:

cut -d. -f2 'some.text'

should be perfectly identical to this:

echo 'some.text' | cut -d. -f2

as we send some.text to STDIN. In first case through a paramter, in second case via pipe.

Where do parameter some.text from the first sample go, if not to STDIN?

jw013
  • 51,212

5 Answers5

1

No, the parameters passed via the command line are not passed to STDIN automatically but are given in separate variables to the program which then can interpret them as it want. The variables are normally called argc and argv.

A lot of the Unix tools (cut, head, tail, cat, ...) can either get there input eithervia STDIN or via files at the command line.

So echo 'some.text' | cut -d. -f2 has the same effect as

echo 'some.text' > temporary_file
cut -d. -f2 temporary_file
rm temporary_file
jofel
  • 26,758
1

Command line arguments and program input are not at all equivalent. They do not travel through the same route and they are interpreted differently.

The command cut interprets arguments that starts with - as options and others as input file names (like many other commands).

Arguments are passed to a program through the execve system call. The environment is passed next to it. The program can read its arguments from a certain location in memory; programming languages usually provide a variable or function to access the arguments, such as the argv parameter to main in C.

Standard input is file descriptor 0. This is, by convention, a pre-opened file that programs can read from. When you run a command on a terminal without redirecting its input, the standard input comes from a terminal (it's what you type). When you redirect the input from a file with mycommand <input_file, the standard input is connected to that file. When you run a command in a pipeline command1 | command2, the standard input of command2 is connected to the standard input of command2.

0

If you are referring to command line arguments (e.g., -d -f2, and in your first example 'some.text'), they all go to the associated program.

Command line arguments may also contain information that specifies where output goes to or input comes from. E.g., in the first example the presence of a filename indicates that the data comes from a file, while in the second example the lack of a filename implies input from stdin. In both cases, output goes to stdout.

Levon
  • 11,384
  • 4
  • 45
  • 41
0

Parameters (aka “command line arguments”) go to a array of strings (char *argv[] in C) which is passed to the entry point function of the program. They're not passed via STDIN or any other pipe.

Some command line tools may accept some arguments also on STDIN (although I cannot think of any right now).

0

STDIN and the program command ( including arguments ) are completely different things. STDIN is a file that the program can read from. It may be connected to a terminal, a disk file, a device, a socket, etc. The program command is simply a set of strings, the first of which is the name of the program. They are passed as arguments to the program's main() function.

psusi
  • 17,303