Jump to content

small confusion about redirection in linux

Go to solution Solved by Eigenvektor,
5 minutes ago, shivajikobardan said:

But > was ignored. Isn't it?

No, it wasn't ignored. That's why the file was cleared. But your "zcho" doesn't produce any output, so there's nothing new to write into the file, while "echo" would.

 

It sees the >, clears the file, then tries to execute "zcho whatever", that fails, so there's no output to redirect.

 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
https://linustechtips.com/topic/1512862-small-confusion-about-redirection-in-linux/
Share on other sites

Link to post
Share on other sites

22 minutes ago, shivajikobardan said:

So can you explain how 

zcho It is cold today! > winter.txt

command processes internally?

To my knowledge what happens is like you say, but not with the arguments you say. The redirection is ignored for argument counting, it sees the two arguments ("zcho" and "It is cold today") and then tries to execute "zcho", which doesn't exist and thus crashes. As to why the file is still empty afterwards, that is explained in the section directly after:

Quote

output file is erased

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!

With "effectively removes the redirection" they don't mean that only > is removed, but that the entire "> winter.txt" is ignored for the sake of argument counting.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to post
Share on other sites

5 minutes ago, shivajikobardan said:

But > was ignored. Isn't it?

No, it wasn't ignored. That's why the file was cleared. But your "zcho" doesn't produce any output, so there's nothing new to write into the file, while "echo" would.

 

It sees the >, clears the file, then tries to execute "zcho whatever", that fails, so there's no output to redirect.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

41 minutes ago, shivajikobardan said:

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.

it sets up the log file (which i what is behind the redirection tag) before processing the rest of the command.

 

in essence, the number of arguments and the redirection operator are entirely disconnected from each other.

Link to post
Share on other sites

1 hour ago, shivajikobardan 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.

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

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

 

 

 

 

see https://man7.org/linux/man-pages/man3/wordexp.3.html to know what happens with the arguments, everything that is not explicitly documented is implementation defined and you shall not rely on it happening in any particular way or order unless stated.

Link to post
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×