polaryeti Posted June 13, 2023 Share Posted June 13, 2023 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. Quote Link to comment Share on other sites More sharing options...
kicken Posted June 13, 2023 Share Posted June 13, 2023 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 Parse the line into it's components Argument list: ['zcho', 'It', 'is', 'cold', 'today!'] Redirections: STDOUT -> winter.txt 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. Extract the first argument and use it as the program/command name (zcho) 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. 1 1 Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 19, 2023 Share Posted June 19, 2023 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. Quote Link to comment Share on other sites More sharing options...
polaryeti Posted June 23, 2023 Author Share Posted June 23, 2023 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 Parse the line into it's components Argument list: ['zcho', 'It', 'is', 'cold', 'today!'] Redirections: STDOUT -> winter.txt 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. Extract the first argument and use it as the program/command name (zcho) 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.