Does the argument post redirect?

Is there any argument after ignoring the redirect, or has any unintended consequences?

I was surprised to find that the typo that I made in my bash script did not matter, because it was specified after the redirect. For instance. I expected him to complain about something like this.

./foo.sh > foo2.log whoops I made a typo

But this does not cause any errors. I have to add a semi colon to make it work as a command and an error, something like

./foo.sh > foo2.log; whoops I made a typo

What else surprised me was that Linux did not refuse after redirecting for example.

./foo.sh > foo2.log whoops I made a typo > command_is_still_going.log

absolutely normal and command_is_still_going.log is still created

Is the “exclamations I made a typo” argument completely ignored, or does this lead to some undesirable behavior? Since I try to use it a lot.

+4
source share
3 answers

Redirects are parsed and processed, and then removed from the command line. Then the remaining line is executed. In other words, redirects can occur anywhere on the command line. As a style, you should put them at the end.

They are equivalent:

./foo.sh > foo2.log whoops I made a typo
./foo.sh whoops I made a typo > foo2.log 

If you foo.shignore your arguments, then the "screams I made a typo" has no effect.

Similarly, they are the same:

./foo.sh > foo2.log whoops I made a typo > command_is_still_going.log
./foo.sh whoops I made a typo > foo2.log > command_is_still_going.log

These are two separate commands:

./foo.sh > foo2.log; whoops I made a typo

./foo.sh > foo2.log
whoops I made a typo
+6
source

You can run:

> set -x
> ./foo.sh > foo2.log whoops I made a typo

And you could see what was actually running:

> + ./foo.sh whoops I made a typo

, foo.sh.

+1

OK, I think I have my answer. Right on Linux, you can do things like echo 'whoops' > foo.log I made a typo this and it will display “screams I made a typo”

This way, essentially, the redirect is simply removed from the string processing, so the unintended result will be something after the redirect processing as an argument to the bash script.

This is proven by making foo.sh simple echo $1. Then run it as it ./foo.sh > foo.log whateverwrites "whatever" as the first argument

0
source

Source: https://habr.com/ru/post/1685892/


All Articles