Jump to content

Small confusion about redirection in Linux


Recommended Posts

https://linux-training.be/funhtml/ch18.html

 

Quote

Note that the bash shell effectively removes the redirection from the command line before argument 0 is executed. This means that in the case of this command:

echo hello > greetings.txt
Quote

the shell only counts two arguments (echo = argument 0, hello = argument 1). The redirection is removed before the argument counting takes place.

I feel it's telling before counting the number of arguments, redirection operator is ignored.

But later it says how it affects output erasing file case.

Quote

While scanning the line, the shell will see the > sign and will clear the file! Since this happens before resolving argument 0, this means that even when the command fails, the file will have been cleared!

[paul@RHELv4u3 ~]$ cat winter.txt 

It is cold today!

[paul@RHELv4u3 ~]$ zcho It is cold today! > winter.txt

-bash: zcho: command not found

[paul@RHELv4u3 ~]$ cat winter.txt 

[paul@RHELv4u3 ~]$

 

So can you explain how 

zcho It is cold today! > winter.txt

command processes internally?

My estimate

1) > is ignored

2) Number of arguments are count. There are 2 arguments "It is cold today!" and winter.txt

3) then what? i don't know.

 

 

Link to comment
Share on other sites

The redirections are part of the setup for running a command.  > and < tie the STDOUT and STDIN streams to a file.  This processing is handled by the shell, not the program being executed, so they are not considered part of the programs argument list.  Since this is pre-execution setup work as well, the redirection happens before the program is executed, thus happen even if the program execution fails.

So, given the command line:

zcho It is cold today! > winter.txt

The shell would

  1. Parse the line into it's components
    • Argument list: ['zcho', 'It', 'is', 'cold', 'today!']
    • Redirections: STDOUT -> winter.txt
  2. Setup STDIN, STDOUT, and STDERR
    • STDIN: tied to the shell's current STDIN stream
    • STDOUT: tied to a new stream created by opening winter.txt for writing (with truncation)
    • STDERR: tied to the shell's current STDERR stream.
  3. Extract the first argument and use it as the program/command name (zcho)
  4. Attempt to execute the program/command with the arguments given

 

You can confirm the redirection happens first by running your invalid command with STDERR redirection:

kicken@web1:~$ zcho It is cold today! 2> error.txt
kicken@web1:~$ cat error.txt
-bash: zcho: command not found

The error message from the zcho command is redirected to the error.txt file rather than displayed in the terminal.

 

  • Like 1
  • Great Answer 1
Link to comment
Share on other sites

On 6/13/2023 at 12:56 PM, polaryeti said:

https://linux-training.be/funhtml/ch18.html

echo hello > greetings.txt

I feel it's telling before counting the number of arguments, redirection operator is ignored.

 

 

No, it is telling you the exact opposite.  Redirection was considered first, which explains why greetings.txt is not an argument counted or sent to the program (echo).

@Kicken provided a really great explanation.

Link to comment
Share on other sites

On 6/14/2023 at 12:25 AM, kicken said:

The redirections are part of the setup for running a command.  > and < tie the STDOUT and STDIN streams to a file.  This processing is handled by the shell, not the program being executed, so they are not considered part of the programs argument list.  Since this is pre-execution setup work as well, the redirection happens before the program is executed, thus happen even if the program execution fails.

So, given the command line:

zcho It is cold today! > winter.txt

The shell would

  1. Parse the line into it's components
    • Argument list: ['zcho', 'It', 'is', 'cold', 'today!']
    • Redirections: STDOUT -> winter.txt
  2. Setup STDIN, STDOUT, and STDERR
    • STDIN: tied to the shell's current STDIN stream
    • STDOUT: tied to a new stream created by opening winter.txt for writing (with truncation)
    • STDERR: tied to the shell's current STDERR stream.
  3. Extract the first argument and use it as the program/command name (zcho)
  4. Attempt to execute the program/command with the arguments given

 

You can confirm the redirection happens first by running your invalid command with STDERR redirection:

kicken@web1:~$ zcho It is cold today! 2> error.txt
kicken@web1:~$ cat error.txt
-bash: zcho: command not found

The error message from the zcho command is redirected to the error.txt file rather than displayed in the terminal.

 

thank you @kicken

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.