0

When I run command, e.g. test -l --hello check:
shell save the command to variables, $0="test", $1="-l", $2="--hello" and $3="check"
shell execute corresponding binary
the binary read variables
the binary do what the variables say

Is standard streams just these variables?


Questions to @philip-couling (I ask here because limits of comments):

I think you are trying to ask about redirection operators and pipes: <, >, |.

Yup, the standard streams is how redirections and pipe works, right?

break up the line of text into arguments

It saves the arguments/flags and also the command itself. For example, ls is the command part and -la is the argument part. This is important e.g. for busybox that is just one binary and often it called with symlinks (if you run e.g. /bin/ls (that is in path so ls) that is symlink to /bin/busybox, busybox binary need the way to know the command and its flags).

pass as an array to the command

As an array? It doesn't use $0..$∞ variables?

When you open a file to read or wrote the OS gives you a "file descriptor" which is just a number. Child processes inherit file descriptors from their parent. Whatever files were open in the parent will also be open and available to the child (unless the file was opened with an explicit instruction not to).

Each file descriptor is just a number. The first three are used for stdin(0) stdout(1) stderr(2). See STDERR_FILENO in posix. As far as I know these are not technically reserved but the fact these three descriptors usually exist prevents others from using the same number.

A software language might have special syntax to write to stdout (like echo or print) but really all the binary is doing is writing to file descriptor number 0.

Oh, now I remember file descriptors and stdin, stdout, stderr are 0, 1, 2. Really good explanation of these! But thew questions, all output of commands goes to 1 and 2, right? So, when will 1 and 2 be cleared?

4
  • 1
    Those are arguments, not standard streams. Could you clarify? I don't see any streams at all mentioned in your question.
    – Chris Down
    Aug 29, 2023 at 1:15
  • @ChrisDown the OP is asking if the streams are passed the same way as program arguments, passing strings into the program. Aug 29, 2023 at 3:42
  • @PhilipCouling or OP is asking if the arguments are stdin? It wouldn't be the first time a user has thought so. OP will have to clarify.
    – muru
    Aug 29, 2023 at 4:33
  • I'm asked, are standard streams just variables. This: "OP is asking if the streams are passed the same way as program arguments". And Philip Couling already answered that question, but a few things remained unclear. @PhilipCouling: can you quote my questions about you answer and answer for them?
    – linuxer
    Sep 17, 2023 at 2:29

1 Answer 1

0

I think you are trying to ask about redirection operators and pipes: <, >, |.

For command line arguments, the shell does break up the line of text into arguments to pass as an array to the command. The shell calls fork to create a new process and then uses exec to start the new command, passing the arguments.

For redirection it's a bit more complicated. The filenames are not directly passed. Instead the standard streams are inherited from the parent process, and the shell must modify them between fork and exec.

When you open a file to read or wrote the OS gives you a "file descriptor" which is just a number. Child processes inherit file descriptors from their parent. Whatever files were open in the parent will also be open and available to the child (unless the file was opened with an explicit instruction not to).

Each file descriptor is just a number. The first three are used for stdin(0) stdout(1) stderr(2). See STDERR_FILENO in posix. As far as I know these are not technically reserved but the fact these three descriptors usually exist prevents others from using the same number.

A software language might have special syntax to write to stdout (like echo or print) but really all the binary is doing is writing to file descriptor number 0.

The way redirection operators work is that the shell opens the file or creates a pipe to get a file descriptor and then calls dup2 to copy the descriptor onto one of the three (0,1,2) before it calls exec.

So the child process is never informed what it has open on stdin, stdout, or stderr. It just has file descriptors which it may be able to get more information about if it interrogates the OS.

1
  • Hi, I have a few questions, please read this edit.
    – linuxer
    Aug 29, 2023 at 17:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .