6

I am looking at a co-workers shell code and I saw this:

date 2&>$0

I know what date does, but what's 2&>$0 doing? He's out for a while, so I can't ask him what this part was about.

  • 3
    Are you sure that's exactly what it says? That is not a normal thing to write. – Michael Homer Jan 7 '15 at 3:29
  • yeah, I think its a stderr redirect F* on his part? I am just looking for a second opinion, since Im not sure what exactly would be happening there. – j0h Jan 7 '15 at 3:33
  • This looks like a gag line (like the famous fork bomb). I seriously doubt this would arise in any real code (and if it does, it is dangerous and makes no sense). Or, it is possible, that he intended to write date 2>/dev/null to produce an errorless date output, and failed spectaculary. – orion Jan 7 '15 at 11:05
  • 4
    Looks to me rather like a typo where "date 2>&0" was intended... – Brian C Jan 7 '15 at 13:10
8

Summary

Under bash, if that command is in a script, the script file will be overwritten with an error message.

Example

Consider the script:

$ cat test.sh
date 2&>$0

Now, run the script:

$ bash test.sh
test.sh: line 2: unexpected EOF while looking for matching ``'
test.sh: line 3: syntax error: unexpected end of file

Observe the new contents of the script:

$ cat test.sh
date: invalid date `2'

Explanation

The command, date 2&>$0, is interpreted as follows:

  1. The date command is run with argument 2

  2. All output, both stdout and stderr, from the date command is redirected to the file $0. $0 is the name of the current script.

    The symbol > indicates redirection of, by default, stdout. As a bash extension, the symbol &> is a shortcut indication redirection of both stdout and stderr. Consequently, both stdout and stderr are redirected to the file $0.

  3. Once the script file is overwritten, it is no longer a valid script and bash will complain about the malformed commands.

Difference between bash and POSIX shells

With a simple POSIX shell, such as dash, the shortcut &> is not supported. Hence, the command date 2&>$0 will redirect only stdout to the file $0. In this case, that means that the script file is overwritten with an empty file while the date error message will appear on the terminal.

  • 2
    …but this does not make any sense ☹ – törzsmókus Jan 7 '15 at 8:01
  • I'd add the important fact that the test shows also that bash interprets the script "line by line" : the original line changes the script itself ($0), and it seems that bash didn't only do that first line: it continued to interpret the new content of the script (line 1 was the "date ..." line, line 2 is probably the new line 1 in the script, the line containing date's error message). As you can see: it interpreted the date output, saw the backquote (around 2), and looked for the matching backquote! – Olivier Dulac Jan 7 '15 at 12:57
10

Assuming that the code you posted is correct, what it does is very strange. It:

  • Runs date 2, which is not a valid invocation of date and which will produce an error message, then
  • Redirects both its standard output and standard error with &>,
  • Into the file containing the running script ($0), erasing its existing contents.

Because the way Bash reads in the script is to take one line at a time from the file, that results in it getting nonsense from the overwritten file and probably exiting (because the file has been truncated to shorter than the point this line appeared) or giving a syntax error (if the original was very short and part of the error is read in as the next line).

I can't think of a single legitimate use for that line, but as you say you've given it exactly as written, that is what it does.


Strictly speaking, it is possible to give another value to $0 and so execute this code non-destructively by invoking Bash with the -c option: bash -c "$(<test.bash)" output-file will put the error message into output-file, but it's utterly perverse to do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.