Web Analytics Made Easy -
StatCounter Small confusion about redirection in Linux - CodingForum

Announcement

Collapse
No announcement yet.

Small confusion about redirection in Linux

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Small confusion about redirection in Linux




    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:


    Code:
    echo hello > greetings.txt
    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.

    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!

    Code:
    [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
    Code:
    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.


  • #2
    Understanding how redirection works can sometimes be really tricky

    Comment


    • #3
      In the case of the command ""zcho It is cold today! > winter.txt,"" here's how it processes internally:
      1. The "">"" redirection operator is recognized by the shell.
      2. The shell clears the file ""winter.txt"" before resolving argument 0, which is ""zcho.""
      3. Since ""zcho"" is not a valid command, you get the ""command not found"" error message.
      4. Even though the command fails, the file ""winter.txt"" remains empty because it was cleared during the shell's processing of the redirection.

      Comment


      • #4
        Originally posted by michellepowell View Post
        In the case of the command ""zcho It is cold today! > winter.txt,"" here's how it processes internally:
        1. The "">"" redirection operator is recognized by the shell.
        2. The shell clears the file ""winter.txt"" before resolving argument 0, which is ""zcho.""
        3. Since ""zcho"" is not a valid command, you get the ""command not found"" error message.
        4. Even though the command fails, the file ""winter.txt"" remains empty because it was cleared during the shell's processing of the redirection.
        So, in summary, your estimate is on point! The redirection operator "">"" is indeed ignored before counting the number of arguments. Then, the shell clears the file before resolving argument 0, ensuring the file will be empty regardless of the command's success or failure.
        Also, check out this windows keys Reddit for some affordable key options.

        Comment

        Working...
        X
        😀
        🥰
        🤢
        😎
        😡
        👍
        👎